将WCF服务的编程配置转换为配置文件
| 我编写了一个简单的WCF Web服务,该服务是通过programmaticaly配置的.它公开了三个端点,它们将不同的绑定绑定到同一个合约: > WebHttpBinding 配置代码目前非常简单: WebServiceHost host = new WebServiceHost(
    typeof(MyService),new Uri("http://localhost:80/"));
host.AddServiceEndpoint(typeof(MyService),new WebHttpBinding(),"");
ServiceEndpoint sbEndpoint = host.AddServiceEndpoint(
    typeof(MyService),new WebHttpRelayBinding(),"http://azureURL");
TransportClientEndpointBehavior sbBehavior =
    new TransportClientEndpointBehavior();
sbBehavior.CredentialType = TransportClientCredentialType.UserNamePassword;
sbBehavior.Credentials.UserName.UserName = "azureUserName";
sbBehavior.Credentials.UserName.Password = "azurePassword";
sbEndpoint.Behaviors.Add(sbBehavior);
host.AddServiceEndpoint(typeof(MyService),new MyBinding(),"http://someURL");
host.Open();现在我想在配置文件中导出此配置,因为我希望能够更改它而无需重新编译. 我现在的问题是: >我在哪里可以找到有价值的信息来实现我的目标?大多数网站只谈论SOAP绑定 – 没有关于如何包含非标准绑定的说法. 解决方法好的,所以最重要的是:>地址 然后抛出一些额外的东西. 1)地址: 从这里得到这个: WebServiceHost host = new WebServiceHost(
    typeof(MyService),new Uri("http://localhost:80/"));
host.AddServiceEndpoint(typeof(MyService),"");和这里: ServiceEndpoint sbEndpoint = host.AddServiceEndpoint(
    typeof(MyService),"http://azureURL");所以你需要这样的东西: <endpoint address="" <endpoint address="http://azureURL" <endpoint address=""http://someURL" 在你的服务中. 2)绑定: 第一个端点是webHttpBinding,第二个端点使用自定义绑定(“MyBinding”) – 所以你有: <endpoint address="" 
          binding="webHttpBinding"
<endpoint address="http://azureURL"
          binding="webRelayHttpBinding"
<endpoint address=""http://someURL"
          binding="myBinding"你需要定义你的自定义绑定: <bindings>
  <customBinding>
    <binding name="MyBinding">
      .. define the parameters of your binding here
    </binding>
  </customBinding>
</bindings>或创建< extensions>存储在单独程序集中的代码中的绑定部分. 3)合同 我没有在任何地方清楚地看到合同 – 你只使用typeof(MyService),但通常,这是具体的服务实例,而不是应该是接口的服务合同(类似于IMyService).你为什么没有明确的服务合同? 无论如何,如果您的服务实现也是合同,同时(不是最佳实践!但可能),那么您有两个端点,如下所示: <endpoint address="" 
          binding="webHttpBinding"
          contract="MyService" />
<endpoint address="http://azureURL"
          binding="webHttpRelayBinding"
          contract="MyService" />
<endpoint address="http://someURL"
          binding="myBinding"
          contract="MyService" />然后你需要在这里和那里添加一些洒水(定义服务的“基地址”,给服务命名等等),最后应该是这样的: <system.serviceModel>
    <bindings>
      <customBinding>
        <binding name="MyBinding">
          .. define the parameters of your binding here
        </binding>
      </customBinding>
    </bindings>
    <services>
      <service name="YourNameSpace.MyService">
        <host>
          <baseAddresses>
             <add baseAddress="http://localhost:80/" />
          </baseAddresses>
         </host>
         <endpoint address="" 
                   binding="webHttpBinding"
                   contract="MyService" />
         <endpoint address="http://azureURL"
                   binding="webHttpRelayBinding"
                   contract="MyService" />
         <endpoint address="http://someURL"
                   binding="myBinding"
                   contract="MyService" />
      </service>
    </services>
</system.serviceModel>现在你所缺少的是所定义的行为 – 我将把它作为海报的练习:-) 这有帮助吗? 至于参考 – 嗯…..很难说….我想通常的书(MLBustamante为初学者/中级学习的“学习WCF”,由Juval Lowy为中级/高级的“编程WCF”)是我最好的打赌,还有很多经验,真的.我不知道任何明确显示和教导如何在代码和配置之间转换设置的来源 – 提到的两本书通常都显示两种方式,从中你可以自己弄清楚. 渣 (编辑:南平站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! | 
