gpt4 book ai didi

c# - 以编程方式定义具有行为的 WCF 端点

转载 作者:行者123 更新时间:2023-11-30 20:20:01 26 4
gpt4 key购买 nike

谁能告诉我如何以编程方式创建具有端点行为的 WSHttpBindings?

这是从 WCF 添加服务对 6 个 Web 服务的引用生成的,我手动编辑这些 Web 服务以添加行为,以便所有端点都使用相同的行为。

<system.serviceModel>
<behaviors>
<endpointBehaviors>
<behavior name="epBehaviour">
<clientCredentials>
<clientCertificate findValue="This is my TEST Cert" storeLocation="LocalMachine" x509FindType="FindBySubjectName" storeName="My" />
</clientCredentials>
</behavior>
</endpointBehaviors>
</behaviors>
<bindings>
<wsHttpBinding>
<binding name="WSHttpBinding_IQuote" messageEncoding="Mtom">
<security mode="TransportWithMessageCredential">
<transport clientCredentialType="None" />
<message clientCredentialType="Certificate" establishSecurityContext="false" />
</security>
</binding>
<binding name="WSHttpBinding_ICommit" messageEncoding="Mtom">
<security mode="TransportWithMessageCredential">
<transport clientCredentialType="None" />
<message clientCredentialType="Certificate" establishSecurityContext="false" />
</security>
</binding>
<binding name="WSHttpBinding_IRetrieve" messageEncoding="Mtom">
<security mode="TransportWithMessageCredential">
<transport clientCredentialType="None" />
<message clientCredentialType="Certificate" establishSecurityContext="false" />
</security>
</binding>
<binding name="WSHttpBinding_IInfo" messageEncoding="Mtom">
<security mode="TransportWithMessageCredential">
<transport clientCredentialType="None" />
<message clientCredentialType="Certificate" establishSecurityContext="false" />
</security>
</binding>
<binding name="WSHttpBinding_IPurge" messageEncoding="Mtom">
<security mode="TransportWithMessageCredential">
<transport clientCredentialType="None" />
<message clientCredentialType="Certificate" establishSecurityContext="false" />
</security>
</binding>
<binding name="WSHttpBinding_ISearch" messageEncoding="Mtom">
<security mode="TransportWithMessageCredential">
<transport clientCredentialType="None" />
<message clientCredentialType="Certificate" establishSecurityContext="false" />
</security>
</binding>
</wsHttpBinding>
</bindings>
<client>
<endpoint address="https://my.service.com/WCFServices/Info.svc" behaviorConfiguration="epBehaviour" binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IInfo" contract="InfoWcfWS.IInfo" name="WSHttpBinding_IInfo" />
<endpoint address="https://my.service.com/WCFServices/Quote.svc" behaviorConfiguration="epBehaviour" binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IQuote" contract="QuoteWcfWS.IQuote" name="WSHttpBinding_IQuote" />
<endpoint address="https://my.service.com/WCFServices/Commit.svc" behaviorConfiguration="epBehaviour" binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_ICommit" contract="CommitWcfWS.ICommit" name="WSHttpBinding_ICommit" />
<endpoint address="https://my.service.com/WCFServices/Retrieve.svc" behaviorConfiguration="epBehaviour" binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IRetrieve" contract="RetrieveWcfWS.IRetrieve" name="WSHttpBinding_IRetrieve" />
<endpoint address="https://my.service.com/WCFServices/Purge.svc" behaviorConfiguration="epBehaviour" binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IPurge" contract="PurgeWcfWS.IPurge" name="WSHttpBinding_IPurge" />
<endpoint address="https://my.service.com/WCFServices/Search.svc" behaviorConfiguration="epBehaviour" binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_ISearch" contract="SearchWcfWS.ISearch" name="WSHttpBinding_ISearch" />
</client>
</system.serviceModel>

目前使用从“添加服务”生成的代理并添加在端点中作为行为配置引用的行为“epBehaviour”,我可以调用端点。

现在,我的下一步是将行为部分和 URL 定义为配置(可能在 web.config 中),这样我就可以为暂存和生产提供不同的配置。

所以我的示例登台配置将具有如下定义的 URL

<appSettings>
<add key="ServiceUrl.Tls12.Quote" value="https://my.service.com/WCFServices/Quote.svc" />
<add key="ServiceUrl.Tls12.Commit" value="https://my.service.com/WCFServices/Commit.svc" />
<add key="ServiceUrl.Tls12.Retrieve" value="https://my.service.com/WCFServices/Retrieve.svc" />
<add key="ServiceUrl.Tls12.Info" value="https://my.service.com/WCFServices/Info.svc" />
<add key="ServiceUrl.Tls12.Purge" value="https://my.service.com/WCFServices/Purge.svc" />
<add key="ServiceUrl.Tls12.Search" value="https://my.service.com/WCFServices/Search.svc" />
</appSettings>

所以如果我正在使用“Info”WebService,我的代码应该是这样的

//Code is missing how to define all the specifics like behaviours.
WSHttpBinding binding = new WSHttpBinding();
EndpointAddress endpoint = new EndpointAddress(new Uri(ConfigurationManager.AppSettings["ServiceUrl.Tls12.Info"]));
InfoWcfWS.InfoClient proxyNew = new InfoWcfWS.InfoClient(binding, endpoint);

总而言之,鉴于“添加服务”后生成的配置,为了生成我的代理类,我想删除 VS 生成的所有配置并定义我自己的配置来保存 URL;端点 URI、行为等其他所有内容都应以编程方式实例化。

谢谢

最佳答案

您必须从 System.ServiceModel.Description 创建您需要的行为实例命名空间或自己创建它们,然后将它们添加到 ChannelFactory .

对于您的示例,您为您想要的界面和您喜欢的任何绑定(bind)创建了一个ChannelFactory。然后实例化一个 ClientCredentials行为,配置您需要的任何设置并将其添加到 EndPointBehaviors ChannelFactory 的。然后您就可以创建一个 clientchannel,它将为您提供一个实现您的服务接口(interface)的对象。您可以像生成的客户端一样使用它。

// binding
var binding = new WSHttpBinding();
// using System.ServiceModel
var channelFactory = new ChannelFactory<InfoWcfWS.IInfo>(binding);
// using System.ServiceModel.Description
var endpointClientbehavior = new ClientCredentials();
endpointClientbehavior.ClientCertificate
.SetCertificate(
"This is my TEST Cert",
StoreLocation.LocalMachine,
StoreName.My);
// add the behavior to the endpoint
channelFactory.Endpoint.EndpointBehaviors.Add(endpointClientbehavior);

// done configuring;
channelFactory.Open();
var endpoint = new EndpointAddress(
new Uri(ConfigurationManager.AppSettings["ServiceUrl.Tls12.Info"]));
// create the clientChannel
var client = channelFactory.CreateChannel(endpoint);
client.Open();
// client implements the operations on InfoWcfWS.IInfo

关于c# - 以编程方式定义具有行为的 WCF 端点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37624516/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com