我编写了一个以编程方式配置的简单 WCF Web 服务。它公开了三个端点,它们将不同的绑定(bind)绑定(bind)到同一个合约:
配置代码目前非常简单:
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();
现在我想将此配置导出到配置文件中,因为我希望能够更改它而无需重新编译。
我现在的问题是:
最佳答案
好的,所以最重要的是:
然后加入一些额外的东西。
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)绑定(bind):
第一个端点是 webHttpBinding,第二个端点使用自定义绑定(bind)(“MyBinding”) - 所以你有:
<endpoint address=""
binding="webHttpBinding"
<endpoint address="http://azureURL"
binding="webRelayHttpBinding"
<endpoint address=""http://someURL"
binding="myBinding"
你需要定义你的自定义绑定(bind):
<bindings>
<customBinding>
<binding name="MyBinding">
.. define the parameters of your binding here
</binding>
</customBinding>
</bindings>
或创建
<extensions>
您的绑定(bind)部分存储在单独程序集中的代码中。3) 契约(Contract)
我在任何地方都没有清楚地看到契约(Contract)——你只使用过 typeof(MyService),但通常,这是具体的服务 实例 ,而不是服务 契约(Contract) 这应该是一个接口(interface)(类似于
IMyService
)。为什么没有明确的服务契约(Contract)?无论如何,如果您的服务实现同时也是契约(Contract)(不是最佳实践!但可能),那么您的两个端点是这样的:
<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 的初学者/中级的“Learning WCF”,Juval Lowy 的中级/高级的“Programming WCF”)是我最好的打赌,还有很多经验,真的。我不知道任何明确显示和教授如何在代码和配置中的设置之间进行转换的来源 - 提到的两本书通常显示两种方式,从这里,你可以自己弄清楚。
马克
关于wcf - 将 WCF 服务的编程配置转换为配置文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1278332/