将WCF服务的编程配置转换为配置文件

Eta*_*tan 8 configuration wcf web-services app-config

我编写了一个简单的WCF Web服务,该服务是通过programmaticaly配置的.它公开了三个端点,它们将不同的绑定绑定到同一个合约:

  • 的WebHttpBinding
  • WebHttpRelayBinding(通过Microsoft azure)
  • myBinding(附加DLL中的自制绑定)

配置代码目前非常简单:

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();
Run Code Online (Sandbox Code Playgroud)

现在我想在配置文件中导出此配置,因为我希望能够在不必重新编译的情况下进行更改.

我现在的问题是:

  • 我在哪里可以找到有价值的信息来实现我的目标?大多数网站只谈论SOAP绑定 - 没有关于如何包含非标准绑定的说法.
  • 我是否必须更改MyBinding才能通过app.config接受配置,或者ServiceModel是否就像我的编程方法在配置正常时那样调用它?

mar*_*c_s 10

好的,所以最重要的是:

  • 地址
  • 捆绑
  • 合同

然后抛出一些额外的东西.

1)地址:

从这里得到这个:

WebServiceHost host = new WebServiceHost(
    typeof(MyService), new Uri("http://localhost:80/"));
host.AddServiceEndpoint(typeof(MyService), new WebHttpBinding(), "");
Run Code Online (Sandbox Code Playgroud)

和这里:

ServiceEndpoint sbEndpoint = host.AddServiceEndpoint(
    typeof(MyService), new WebHttpRelayBinding(), "http://azureURL");
Run Code Online (Sandbox Code Playgroud)

所以你需要这样的东西:

<endpoint address=""
<endpoint address="http://azureURL"
<endpoint address=""http://someURL"
Run Code Online (Sandbox Code Playgroud)

在你的服务中.

2)绑定:

第一个端点是webHttpBinding,第二个端点使用自定义绑定("MyBinding") - 所以你有:

<endpoint address="" 
          binding="webHttpBinding"
<endpoint address="http://azureURL"
          binding="webRelayHttpBinding"
<endpoint address=""http://someURL"
          binding="myBinding"
Run Code Online (Sandbox Code Playgroud)

并且您需要定义自定义绑定:

<bindings>
  <customBinding>
    <binding name="MyBinding">
      .. define the parameters of your binding here
    </binding>
  </customBinding>
</bindings>
Run Code Online (Sandbox Code Playgroud)

或者<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" />
Run Code Online (Sandbox Code Playgroud)

然后你需要在这里和那里添加一些洒水(定义服务的"基地址",给服务命名等等),最后应该是这样的:

<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>
Run Code Online (Sandbox Code Playgroud)

现在你所缺少的是所定义的行为 - 我将把它作为海报的练习:-)

这有帮助吗?

至于参考 - 嗯.....很难说....我猜通常的书(MLBustamante为初学者/中级学习"学习WCF",Juval Lowy为中级/高级的"编程WCF")是我最好的选择,还有很多经验,真的.我不知道任何明确显示和教导如何在代码和配置之间转换设置的来源 - 提到的两本书通常都显示两种方式,从中你可以自己弄清楚.