一个WCF宿住如何发布两个接口
2017-05-26 13:32
267 查看
服务端
建立第一个接口ISrevice1:
[ServiceContract]
public interface IService1
{
[OperationContract]
string GetIService1();
}
实现IService1:
public class Service1 : IService1
{
public string GetIService1()
{
return string.Format("You Called: IService1");
}
}
建立第二个接口IService2:
[ServiceContract]
public interface IService2
{
[OperationContract]
string GetIService2();
实现IService2:
public class Service2 : IService2
{
public string GetIService2()
{
return string.Format("You Called: IService2");
}
}
新建一个WinForm项目,作为宿住,并引用WCF服务,并打开两个接口
private ServiceHost host1 = null;
private ServiceHost host2 = null;
private void button1_Click(object sender, EventArgs e)
{
host1 = new ServiceHost(typeof(WcfServiceLibrary1.Service1));
host2 = new ServiceHost(typeof(WcfServiceLibrary1.Service2));
host1.Open();
host2.Open();
label1.Text = "服务已启动";
}
主要是配置App.config文件:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<runtime>
<legacyUnhandledExceptionPolicy enabled="true" />
</runtime>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="NoneSecurity" closeTimeout="00:01:00" openTimeout="00:01:00"
receiveTimeout="00:10:00" sendTimeout="00:01:00" allowCookies="false"
bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
maxBufferSize="2147483647" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647"
transferMode="Streamed"
useDefaultWebProxy="true">
<readerQuotas maxDepth="64" maxStringContentLength="2147483647"
maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
<security mode="None">
<transport clientCredentialType="None" proxyCredentialType="None"
realm="" />
<message clientCredentialType="UserName" algorithmSuite="Default" />
</security>
</binding>
</basicHttpBinding>
</bindings>
<services>
<service name="WcfServiceLibrary1.Service1" behaviorConfiguration="CalculatorServiceBehavior">
<host>
<baseAddresses>
<add baseAddress="http://127.0.0.1:8000/"/>
</baseAddresses>
</host>
<endpoint address="" binding="basicHttpBinding" bindingConfiguration="NoneSecurity" contract="WcfServiceLibrary1.IService1">
</endpoint>
</service>
<service name="WcfServiceLibrary1.Service2" behaviorConfiguration="CalculatorServiceBehavior">
<host>
<baseAddresses>
<add baseAddress="http://127.0.0.1:8001/"/>
</baseAddresses>
</host>
<endpoint address="" binding="basicHttpBinding" bindingConfiguration="NoneSecurity" contract="WcfServiceLibrary1.IService2">
</endpoint>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="CalculatorServiceBehavior">
<serviceTimeouts />
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="true"/>
<dataContractSerializer maxItemsInObjectGraph="2147483647"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
注意,配置文件中插入了两个service节点,分别对应两个接口。
以上服务端搭建完成。
客户端:
建立WinForm项目,分别引用两个服务,并命名为Service1、Service2:
private void button1_Click(object sender, EventArgs e)
{
Service1.IService1 service1 = new Service1.Service1Client();
MessageBox.Show(service1.GetIService1());
service1 = null;
}
private void button2_Click(object sender, EventArgs e)
{
Service2.IService2 service2 = new Service2.Service2Client();
MessageBox.Show(service2.GetIService2());
service2 = null;
}
客户端配置文件在引用后自动生成
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_IService1" closeTimeout="00:01:00"
openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
useDefaultWebProxy="true">
<readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
maxBytesPerRead="4096" maxNameTableCharCount="16384" />
<security mode="None">
<transport clientCredentialType="None" proxyCredentialType="None"
realm="" />
<message clientCredentialType="UserName" algorithmSuite="Default" />
</security>
</binding>
<binding name="BasicHttpBinding_IService2" closeTimeout="00:01:00"
openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
useDefaultWebProxy="true">
<readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
maxBytesPerRead="4096" maxNameTableCharCount="16384" />
<security mode="None">
<transport clientCredentialType="None" proxyCredentialType="None"
realm="" />
<message clientCredentialType="UserName" algorithmSuite="Default" />
</security>
</binding>
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://127.0.0.1:8000/" binding="basicHttpBinding"
bindingConfiguration="BasicHttpBinding_IService1" contract="Service1.IService1"
name="BasicHttpBinding_IService1" />
<endpoint address="http://127.0.0.1:8001/" binding="basicHttpBinding"
bindingConfiguration="BasicHttpBinding_IService2" contract="Service2.IService2"
name="BasicHttpBinding_IService2" />
</client>
</system.serviceModel>
</configuration>
运行服务端及客户端,实现客户端调用两个接口。
此示例只是简单的实现两个接口的过程,实际上两个接口可以使用不同的协议通讯。
建立第一个接口ISrevice1:
[ServiceContract]
public interface IService1
{
[OperationContract]
string GetIService1();
}
实现IService1:
public class Service1 : IService1
{
public string GetIService1()
{
return string.Format("You Called: IService1");
}
}
建立第二个接口IService2:
[ServiceContract]
public interface IService2
{
[OperationContract]
string GetIService2();
实现IService2:
public class Service2 : IService2
{
public string GetIService2()
{
return string.Format("You Called: IService2");
}
}
新建一个WinForm项目,作为宿住,并引用WCF服务,并打开两个接口
private ServiceHost host1 = null;
private ServiceHost host2 = null;
private void button1_Click(object sender, EventArgs e)
{
host1 = new ServiceHost(typeof(WcfServiceLibrary1.Service1));
host2 = new ServiceHost(typeof(WcfServiceLibrary1.Service2));
host1.Open();
host2.Open();
label1.Text = "服务已启动";
}
主要是配置App.config文件:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<runtime>
<legacyUnhandledExceptionPolicy enabled="true" />
</runtime>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="NoneSecurity" closeTimeout="00:01:00" openTimeout="00:01:00"
receiveTimeout="00:10:00" sendTimeout="00:01:00" allowCookies="false"
bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
maxBufferSize="2147483647" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647"
transferMode="Streamed"
useDefaultWebProxy="true">
<readerQuotas maxDepth="64" maxStringContentLength="2147483647"
maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
<security mode="None">
<transport clientCredentialType="None" proxyCredentialType="None"
realm="" />
<message clientCredentialType="UserName" algorithmSuite="Default" />
</security>
</binding>
</basicHttpBinding>
</bindings>
<services>
<service name="WcfServiceLibrary1.Service1" behaviorConfiguration="CalculatorServiceBehavior">
<host>
<baseAddresses>
<add baseAddress="http://127.0.0.1:8000/"/>
</baseAddresses>
</host>
<endpoint address="" binding="basicHttpBinding" bindingConfiguration="NoneSecurity" contract="WcfServiceLibrary1.IService1">
</endpoint>
</service>
<service name="WcfServiceLibrary1.Service2" behaviorConfiguration="CalculatorServiceBehavior">
<host>
<baseAddresses>
<add baseAddress="http://127.0.0.1:8001/"/>
</baseAddresses>
</host>
<endpoint address="" binding="basicHttpBinding" bindingConfiguration="NoneSecurity" contract="WcfServiceLibrary1.IService2">
</endpoint>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="CalculatorServiceBehavior">
<serviceTimeouts />
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="true"/>
<dataContractSerializer maxItemsInObjectGraph="2147483647"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
注意,配置文件中插入了两个service节点,分别对应两个接口。
以上服务端搭建完成。
客户端:
建立WinForm项目,分别引用两个服务,并命名为Service1、Service2:
private void button1_Click(object sender, EventArgs e)
{
Service1.IService1 service1 = new Service1.Service1Client();
MessageBox.Show(service1.GetIService1());
service1 = null;
}
private void button2_Click(object sender, EventArgs e)
{
Service2.IService2 service2 = new Service2.Service2Client();
MessageBox.Show(service2.GetIService2());
service2 = null;
}
客户端配置文件在引用后自动生成
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_IService1" closeTimeout="00:01:00"
openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
useDefaultWebProxy="true">
<readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
maxBytesPerRead="4096" maxNameTableCharCount="16384" />
<security mode="None">
<transport clientCredentialType="None" proxyCredentialType="None"
realm="" />
<message clientCredentialType="UserName" algorithmSuite="Default" />
</security>
</binding>
<binding name="BasicHttpBinding_IService2" closeTimeout="00:01:00"
openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
useDefaultWebProxy="true">
<readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
maxBytesPerRead="4096" maxNameTableCharCount="16384" />
<security mode="None">
<transport clientCredentialType="None" proxyCredentialType="None"
realm="" />
<message clientCredentialType="UserName" algorithmSuite="Default" />
</security>
</binding>
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://127.0.0.1:8000/" binding="basicHttpBinding"
bindingConfiguration="BasicHttpBinding_IService1" contract="Service1.IService1"
name="BasicHttpBinding_IService1" />
<endpoint address="http://127.0.0.1:8001/" binding="basicHttpBinding"
bindingConfiguration="BasicHttpBinding_IService2" contract="Service2.IService2"
name="BasicHttpBinding_IService2" />
</client>
</system.serviceModel>
</configuration>
运行服务端及客户端,实现客户端调用两个接口。
此示例只是简单的实现两个接口的过程,实际上两个接口可以使用不同的协议通讯。
相关文章推荐
- 假设一个网站系统存在两个配置文件web.config和config.config,那么我如何读取config.config的配置节呢?System.Configuration名字空间下的接口好象只能读
- WCF技术剖析之二十七: 如何将一个服务发布成WSDL[基于WS-MEX的实现](提供模拟程序)
- 一个类如何实现两个接口中同名同参数不同返回值的函数
- WCF技术剖析之二十七: 如何将一个服务发布成WSDL[编程篇]
- WCF技术剖析之二十七: 如何将一个服务发布成WSDL[基于HTTP-GET的实现](提供模拟程序)
- WCF技术剖析之二十七: 如何将一个服务发布成WSDL[基于WS-MEX的实现](提供模拟程序)
- 一个类如何实现两个接口中同名同参数不同返回值的函数
- 如何建立一个WCF服务并将其发布到IIS上
- WCF技术剖析之二十七: 如何将一个服务发布成WSDL[基于HTTP-GET的实现](提供模拟程序)
- WCF技术剖析之二十七: 如何将一个服务发布成WSDL[编程篇]
- 如何完成一个实现Pause和Continue这两个功能的Windows Service
- 谈JAVA中如何将两个byte数组合成一个数组
- WCF for .NET CF的一个应用及两个困惑的问题
- 两个并排的DIV 左面一个的width=175 右面的那个DIV如何自适应不同分辨率填充满屏幕
- 大家说说,做一个在两个数据库之间同步数据的接口需要几天?
- 如何将一个表单同时提交到两个地方处理
- 一个FORM表单中有两个SUBMIT按钮,如何区分是处理
- 为DataGrid写一个Renderer需要注意实现的两个接口:IListDataRenderer和IDropInListDataRenderer
- 如何发布一个ASP.NET网站
- javascript操作两个选择列表(有两个列表,如何实现在一个列表通过双击和多选列表中内容添加到另一个列表. )