Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/268.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 以编程方式使用行为定义WCF端点_C#_Web Services_Wcf - Fatal编程技术网

C# 以编程方式使用行为定义WCF端点

C# 以编程方式使用行为定义WCF端点,c#,web-services,wcf,C#,Web Services,Wcf,有人能告诉我如何以编程方式创建具有端点行为的WSHttpBinding吗 这是从WCF向6个Web服务添加服务引用生成的,我手动编辑这些Web服务以添加行为,以便所有端点使用相同的行为 您必须从名称空间中创建所需行为的实例,或者自己创建实例,然后将它们添加到一个名称空间中 在您的示例中,您为所需的接口和您喜欢的任何绑定创建了一个ChannelFactory。然后实例化一个行为,对其配置所需的任何设置,并将其添加到ChannelFactory的。然后,您可以创建一个clientchannel,它

有人能告诉我如何以编程方式创建具有端点行为的WSHttpBinding吗

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


您必须从名称空间中创建所需行为的实例,或者自己创建实例,然后将它们添加到一个名称空间中

在您的示例中,您为所需的接口和您喜欢的任何绑定创建了一个
ChannelFactory
。然后实例化一个行为,对其配置所需的任何设置,并将其添加到ChannelFactory的。然后,您可以创建一个clientchannel,它将为您提供一个实现服务接口的对象。您可以将其用作生成的客户端

// 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
//绑定
var binding=新的WSHttpBinding();
//使用System.ServiceModel
var channelFactory=新的channelFactory(绑定);
//使用System.ServiceModel.Description
var endpointClientbehavior=new ClientCredentials();
endpointClientbehavior.ClientCertificate
.SetCertificate(
“这是我的考试证书”,
StoreLocation.LocalMachine,
StoreName.My);
//将行为添加到端点
channelFactory.Endpoint.EndpointBehaviors.Add(endpointClientbehavior);
//完成配置;
channelFactory.Open();
var endpoint=新的端点地址(
新的Uri(ConfigurationManager.AppSettings[“ServiceUrl.Tls12.Info”]);
//创建clientChannel
var client=channelFactory.CreateChannel(端点);
client.Open();
//客户端在InfoWcfWS.IInfo上实现操作

最后,我找到了一种更简单的方法来实现这一点——这是我正在访问的方法信息之一:

WSHttpBinding binding = new WSHttpBinding();
EndpointAddress endpoint = new EndpointAddress(new Uri("https://my.service.com/WCFServices/Info.svc"));

binding.Name = "WSHttpBinding_IInfo";
binding.MessageEncoding = WSMessageEncoding.Mtom;
binding.Security = new WSHttpSecurity();
binding.Security.Mode = SecurityMode.TransportWithMessageCredential;
binding.Security.Transport = new HttpTransportSecurity();
binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.None;
binding.Security.Message = new NonDualMessageSecurityOverHttp();
binding.Security.Message.ClientCredentialType = MessageCredentialType.Certificate;
binding.Security.Message.EstablishSecurityContext = false;

InfoClient proxy = new InfoClient(binding, endpoint);

proxy.ClientCredentials.ClientCertificate.SetCertificate(StoreLocation.LocalMachine, StoreName.My,                                              X509FindType.FindBySubjectName, "This is my TEST Cert");

object response = proxy.ServiceMethod();

如果我理解正确,那么您希望手动创建客户端代理。我认为您可以控制服务器和客户端。我说的对吗?在你的代码片段的末尾,你提到我可以访问接口上的操作,但是,我需要访问一个部分类,该类继承了包含我的方法的接口,该接口有2个参数,即InfoWcfWS.InfoClient proxyNew=new InfoWcfWS.InfoClient(绑定,端点);其中InfoClient是一个部分类,它继承了CLietnBase和IInfo,并具有一系列公共方法。在我的示例中,我忽略生成的ClientBase,并为您提供对接口的完全控制。在我的代码示例中,仍然有两个参数:binding和来自配置文件的Uri。您不再需要生成的客户端。谢谢,稍后我将探索channelFactory,因为它似乎是访问任何类型服务的更通用的方式。。。现在,我已经探讨了直接访问代理类生成的InfoClient方法的最初目标。