c# – 作为自己的客户端的服务

c# – 作为自己的客户端的服务,第1张

概述考虑这种情况: > WCF服务已启动并正在运行. >服务需要每隔一段时间调用一次. 我现在所做的是为同一服务添加服务引用,并在服务配置文件中添加一个额外的端点客户端.在net.tcp上工作. 它工作正常,但我在某处读到你可以使用“进程中”托管来连接到服务而不使用代理.这样您就可以摆脱配置并获得更清晰的代码. 因此,使用相应的配置设置而不是这个: DeliveryOwnClient.Delivery 考虑这种情况:

> WCF服务已启动并正在运行.
>服务需要每隔一段时间调用一次.

我现在所做的是为同一服务添加服务引用,并在服务配置文件中添加一个额外的端点客户端.在net.tcp上工作.

它工作正常,但我在某处读到你可以使用“进程中”托管来连接到服务而不使用代理.这样您就可以摆脱配置并获得更清晰的代码.

因此,使用相应的配置设置而不是这个:

DeliveryOwnClIEnt.DeliveryClIEnt deliveryObject = new DeliveryOwnClIEnt.DeliveryClIEnt("netTcpDeliveryService");

我想在没有任何配置的情况下使用它:

IDelivery deliveryObject = InProcessFactory.CreateInstance<DeliveryService.Delivery,IDelivery>();

但是这引发了一个异常,“带有合同的http://localhost:8003/DeliveryService的Channeldispatcher”IMetadataExchange“无法打开IChannelListener.对于Uri net.tcp:// localhost:9003 / DeliveryService已经存在注册”

CreateInstance的实现如下所示:

ServiceHost host = new ServiceHost(typeof(S));string address = "net.pipe://" + Environment.Machinename + "/" + GuID.NewGuID();host.AddServiceEndpoint(typeof(I),Binding,address);host.open();

所以我添加了一个net.pipe baseaddress,它失败了,因为net.tcp上已经有了一些东西.

**编辑**

至少弄清楚为什么会发生这种情况.

该服务在app.config中配置了两个baseaddresses

<service name="DeliveryService.Delivery">     <endpoint binding="netTcpBinding" contract="DeliveryService.IDelivery"/>     <host>       <baseAddresses>         <add baseAddress="http://localhost:8003/DeliveryService" />         <add baseAddress="net.tcp://localhost:9003/DeliveryService" />       </baseAddresses>     </host>   </service>

当主机构建时

ServiceHost host = new ServiceHost(typeof(S));

它在配置文件中找到该部分并自动添加net.tcp和http基地址.
我添加net.pipe,但这没关系.当服务打开时,它发现net.tcp已经在运行,所以它不会继续.

所以我想我的问题变成了:是否有可能在没有读取app.config的情况下构建ServiceHost?

解决方法 周杰伦设法搞清楚了! ServiceHost派生自ServiceHostBase,该类具有名为ApplyConfiguration的虚函数.所以我创建了一个派生自ServiceHost的类并重写了ApplyConfiguration …并将其保留为空.

class ServiceHostNoConfig<S> : ServiceHost where S : class{    public ServiceHostNoConfig(string address)    {        UriSchemeKeyedCollection c = new UriSchemeKeyedCollection(new Uri(address));        InitializeDescription(typeof(S),c);    }    public new voID InitializeDescription(Type serviceType,UriSchemeKeyedCollection baseAddresses)    {        base.InitializeDescription(serviceType,baseAddresses);    }    protected overrIDe voID ApplyConfiguration()    {    }}

像这样使用它:

ServiceHost host = new ServiceHostNoConfig<S>(address);        host.AddServiceEndpoint(typeof(I),address);
总结

以上是内存溢出为你收集整理的c# – 作为自己的客户端的服务全部内容,希望文章能够帮你解决c# – 作为自己的客户端的服务所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/langs/1232200.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-06-06
下一篇2022-06-06

发表评论

登录后才能评论

评论列表(0条)

    保存