Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/326.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# ChannelFactory.CreateChannel()确实打开了连接吗?_C#_Wcf_Proxy_Channelfactory - Fatal编程技术网

C# ChannelFactory.CreateChannel()确实打开了连接吗?

C# ChannelFactory.CreateChannel()确实打开了连接吗?,c#,wcf,proxy,channelfactory,C#,Wcf,Proxy,Channelfactory,也许这是非常明显的,但在谷歌做了很多之后,无法得出任何结论 我想知道“ChannelFactory.CreateChannel()是否真的打开了连接,或者它只是返回了一些东西,并且在方法调用时实际的连接将被打开。如果我不关闭它,这个连接将保持多长时间。”只有在ChannelFactory中调用open()时,连接才会被打开。 这里的示例部分演示了这一点。它认为您只需要创建以下内容: // Sample service public class Service : IService { pu

也许这是非常明显的,但在谷歌做了很多之后,无法得出任何结论


我想知道“ChannelFactory.CreateChannel()是否真的打开了连接,或者它只是返回了一些东西,并且在方法调用时实际的连接将被打开。如果我不关闭它,这个连接将保持多长时间。”

只有在ChannelFactory中调用open()时,连接才会被打开。
这里的示例部分演示了这一点。

它认为您只需要创建以下内容:

// Sample service
public class Service : IService
{
   public void SendMessage(string message)
   {
      // do the processing....
   }
}

// Creating client connection using factory
// I can't remember the used of my asterisk here but this is use to identity the configuration name used for the endpoint.
var result = new ChannelFactory<IService>("*", new EndpointAddress(serviceAddress));
IService yourService = result.CreateChannel();

// This will automatically open a connection for you.
yourService.SendMessage("It works!");

// Close connection
result.Close();
//示例服务
公共课服务:IService
{
公共无效发送消息(字符串消息)
{
//进行处理。。。。
}
}
//使用工厂创建客户端连接
//我不记得我的星号在这里的用法,但它用于标识端点使用的配置名称。
var结果=新的ChannelFactory(“*”,新的端点地址(serviceAddress));
IService yourService=result.CreateChannel();
//这将自动为您打开连接。
yourService.SendMessage(“它起作用了!”);
//密切联系
result.Close();
只需介绍一下我的多个端点的客户端配置:

<client>
      <!--note that there is no address on the endpoints as it will be overridden by the app anyway-->
      <endpoint binding="wsHttpBinding" bindingConfiguration="wsHttpBinding" behaviorConfiguration="standardBehavior" contract="IService" name="Service"/>
       .
       .
</client>

.
.
我对我的客户端使用这种方法来连接IIS中托管的30多个服务。顺便说一句,我只是将这段代码带到我现有的WCF服务中,它的实际实现是,将它包装到另一个方法中,在这个方法中,我可以简单地将我的服务作为泛型类型和服务地址传递


我在这里使用了消息模式Request-Reply和.NET4.5。

问得好。当我想知道类似的事情时,我只是在阅读.Net的源代码

CreateChannel
方法在内部调用
Open
方法。如果
CommunicationState
不等于
Open
,则执行
DefaultOpenTimeout
Open
方法

DefaultOpenTimeout
由端点绑定配置配置


你可以看到。

我试过这个例子。。。但是我如何从这个通道调用我的服务(它是IRequestChannel类型);var factory=binding.BuildChannelFactory(binding,myEndpoint);factory.Open();var channel=factory.CreateChannel(myEndpoint);var data=GenerateParameters();通道打开();我不想动态生成消息。谢谢您的回复。只是一个问题,如果我按你说的方式打电话给服务部。。将调用哪个服务方法。我找不到任何方法来提及方法名。提前谢谢。我通过
yourService.SendMessage(“它起作用了!”)打电话给
public void SendMessage(string message)