2009-08-14 119 views
8

我已经编写了一个配置为programmaticaly的简单WCF Web服务。它暴露了结合不同绑定到相同的合同三个端点:将WCF服务的编程配置转换为配置文件

  • 的WebHttpBinding
  • WebHttpRelayBinding(超过微软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(); 

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

我在这一刻的问题是:

  • 我在哪里可以找到有价值的信息,从而实现我的目标?大多数网站只是谈论SOAP绑定 - 没有关于如何包含非标准绑定的信息。
  • 我是否必须更改MyBinding才能通过app.config接受配置,或者ServiceModel如何调用它与编程方式完全相同?

回答

11

OK,所以最重要的事情是:

  • 地址
  • 结合
  • 合同

,然后一些额外的东西扔在

1)。地址:

从这里得到这样的:

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"); 

,所以你需要这样的东西:你的服务

<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编程的“Programming WCF”为中级/高级)是我最好的选择,而且真的有很多经验。我不知道任何明确显示和指导如何在代码和配置设置之间进行转换的源代码 - 提到的两本书通常都会显示出两种方式,您可以自己弄清楚。

Marc

+0

感谢您解释构建配置文件的过程。如您所料,服务实施也是合同。我选择了这个,因为合同只包含两个非常通用的功能,并且不可重用。我刚刚尝试过使用webHttpBinding并将现有的程序化设置注释掉。但是,它不起作用。当我在我的Web浏览器中打开http:// localhost时,服务无法访问。我是否必须为VS中的应用程序做一些其他设置? (这是目前标准的“控制台应用程序”。) – Etan 2009-08-14 15:23:30

+0

嗯,你有一个问题是:对于webHttp绑定,你需要WebServiceHost - 对于其他绑定,你需要直接的ServiceHost - 所以你可能不会能够在单个(Web)ServiceHost中托管所有端点 – 2009-08-14 15:39:38

1

配置文件未被正确识别的问题可以解决。

就不得不添加

ServiceHost h = new ServiceHost(typeof(MyService)); 
h.Open(); 

我的代码,我认为ServiceModel将自动启动该服务,因为它知道的所有信息。 在配置文件中添加关于“MyService”的信息并且之后还必须在代码中指定它,这在某种程度上很奇怪。

但是,我的问题真正的答案是由marc_s给出的,他描述了从编程方法到配置文件转换的整个过程。