我编写了一个简单的WCF Web服务,该服务是通过programmaticaly配置的。它公开了三个端点,它们将不同的绑定绑定到同一个合同:
目前配置代码非常简单:
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();
现在我想在配置文件中导出此配置,因为我希望能够更改它而无需重新编译。
此刻我的问题是:
答案 0 :(得分:10)
好的,所以最重要的是:
然后抛出一些额外的东西。
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的“编程WCF”为中级/高级)是我最好的选择,还有很多经验,真的。我不知道任何明确显示和教导如何在代码和配置之间进行转换的来源 - 提到的两本书通常都显示两种方式,从中你可以自己解决。
马克
答案 1 :(得分:1)
可以解决配置文件无法正确识别的问题。
只需添加
ServiceHost h = new ServiceHost(typeof(MyService));
h.Open();
到我的代码,我认为ServiceModel会自动启动服务,因为它知道所有信息。 将配置文件中的“MyService”信息添加到配置文件中,然后还必须在代码中指定它,这有点奇怪。
然而,我的问题的真正答案是由marc_s给出的,他描述了从编程方法转换为配置文件的整个过程。