【问题标题】:Self Hosting WCF service using app.Config files使用 app.Config 文件的自托管 WCF 服务
【发布时间】:2018-07-01 06:09:24
【问题描述】:

我有自托管 WCF 服务,其中包含它自己的 app.Config 以公开服务合同所需的端点。如果该服务在programs.cs main 方法中启动,则一切正常,并且元数据通过浏览器公开。但是,我基于 ServiceBase 类创建了一个 HostService 类,该类位于同一主机库中,并在 program.cs 文件中实例化。 HostService 类启动服务,并有一个计时器方法来 ping 其他客户端 Web 服务以获取信息。

我的问题是,当我创建 HostService : ServiceBase 类并从 main() 实例化它时,我必须在服务库中放置一个重复的 app.Config 文件,以便端点正确公开并返回元数据/wsdl。如果可能,我不想维护 2 个重复的 app.config 文件。目前主机库和服务库都需要一个。有没有办法只有一个主机可以同时用于两者?抱歉这个愚蠢的问题,但我是 WCF 的新手 =)

程序.cs

static void Main(string[] args){
  var service = new HostService(); 
  service.StartHostService(args);   
}

HostService.cs

public partial class HostService : ServiceBase
{
    internal void StartHostService(string[] args)
    {
        this.OnStart(args);
        Console.ReadLine();
        this.OnStop();
    }

    ....
}

【问题讨论】:

    标签: c# .net wcf


    【解决方案1】:

    简短的回答是否定的。必须有两种配置,一种用于使用 WCF 的客户端,另一种用于公开与 WCF 的通信方法的服务器。

    为了让您的客户端正常工作,您的配置必须设置为 Client Configuration

    <?xml version="1.0" encoding="utf-8"?>  
    <configuration>  
      <system.serviceModel>  
            <client>  
              <endpoint  
                name="endpoint1"  
                address="http://localhost/ServiceModelSamples/service.svc"  
                binding="wsHttpBinding"  
                bindingConfiguration="WSHttpBinding_IHello"  
                behaviorConfiguration="IHello_Behavior"  
                contract="IHello" >  
    
                <metadata>  
                  <wsdlImporters>  
                    <extension  
                      type="Microsoft.ServiceModel.Samples.WsdlDocumentationImporter, WsdlDocumentation"/>  
                  </wsdlImporters>  
                </metadata>  
    
                <identity>  
                  <servicePrincipalName value="host/localhost" />  
                </identity>  
              </endpoint>  
    // Add another endpoint by adding another <endpoint> element.  
              <endpoint  
                name="endpoint2">  
               //Configure another endpoint here.  
              </endpoint>  
            </client>  
    
    //The bindings section references by the bindingConfiguration endpoint attribute.  
        <bindings>  
          <wsHttpBinding>  
            <binding name="WSHttpBinding_IHello"   
                     bypassProxyOnLocal="false"   
                     hostNameComparisonMode="StrongWildcard">  
              <readerQuotas maxDepth="32"/>  
              <reliableSession ordered="true"   
                               enabled="false" />  
              <security mode="Message">  
               //Security settings go here.  
              </security>  
            </binding>  
            <binding name="Another Binding"  
            //Configure this binding here.  
            </binding>  
              </wsHttpBinding>  
            </bindings>  
    
    //The behavior section references by the behaviorConfiguration endpoint attribute.  
            <behaviors>  
                <endpointBehaviors>  
                    <behavior name=" IHello_Behavior ">  
                        <clientVia />  
                    </behavior>  
                </endpointBehaviors>  
            </behaviors>  
    
        </system.serviceModel>  
    </configuration> 
    

    注意 &lt;client&gt; 标记指定客户端必须如何调用 WCF。

    Server Config

    <?xml version="1.0" encoding="utf-8"?>  
    <configuration>  
     <system.serviceModel>  
      <bindings>  
        <basicHttpBinding>  
         <binding name="myBindingConfiguration1" closeTimeout="00:01:00" />  
         <binding name="myBindingConfiguration2" closeTimeout="00:02:00" />  
         <binding closeTimeout="00:03:00" />  <!—- Default binding for basicHttpBinding -->  
        </basicHttpBinding>  
         </bindings>  
         <services>  
          <service name="MyNamespace.myServiceType">  
           <endpoint   
              address="myAddress" binding="basicHttpBinding"   
              bindingConfiguration="myBindingConfiguration1"  
              contract="MyContract"  />  
           <endpoint   
              address="myAddress2" binding="basicHttpBinding"   
              bindingConfiguration="myBindingConfiguration2"  
              contract="MyContract" />  
           <endpoint   
              address="myAddress3" binding="basicHttpBinding"   
              contract="MyContract" />  
           </service>  
          </services>  
        </system.serviceModel>  
    </configuration>  
    

    注意这里没有&lt;client&gt; 标签。

    【讨论】:

    • 完美,非常感谢。我的实现是自托管服务,而不是带有 .svc 服务文件的 IIS 类型服务,这有关系吗?客户端标签仍然适用于这两种类型的实现吗?我认为如果是 IIS 类型,.scv 服务端会有 Web.Config 而不是 App.config,对吗?
    • 另外,奇怪的是,如果我在 Program.cs 主方法中启动服务,我不需要服务端的 App.Config,因为它暴露了端点并且工作正常,它是就在我在主机端创建一个 serviceBase 类型类时,它启动了从它所做的 main() 调用的服务。为什么你觉得有什么不同?
    • 确实如此,因为客户端必须同时具有客户端和服务标签:) 客户端必须知道要使用哪些端点。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-07-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-07
    相关资源
    最近更新 更多