一个WCF宿住如何发布两个接口

服务端

建立第一个接口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>

运行服务端及客户端,实现客户端调用两个接口。

此示例只是简单的实现两个接口的过程,实际上两个接口可以使用不同的协议通讯。

阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/wind_3278/article/details/72765719
文章标签: wcf
个人分类: DotNet
想对作者说点什么? 我来说一句

WCF引用服务和直接用接口的区别

2015年07月28日 101KB 下载

没有更多推荐了,返回首页

不良信息举报

一个WCF宿住如何发布两个接口

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭