[英]A service acting as client of its own
Consider this scenario: 考虑这种情况:
What I did now is add a service reference to the same service and added an extra endpoint + client in the service configuration file. 我现在所做的是为同一服务添加服务引用,并在服务配置文件中添加额外的端点+客户端。 Working over net.tcp.
在net.tcp上工作。
It works fine, but I read somewhere that you can use "in process" hosting to connect to a service without using a proxy. 它工作正常,但我在某处读到你可以使用“进程中”托管来连接到服务而不使用代理。 This way you can get rid of the configuration and have much cleaner code.
这样您就可以摆脱配置并获得更清晰的代码。
So instead of this with the accompying configuration settings: 因此,使用相应的配置设置而不是这个:
DeliveryOwnClient.DeliveryClient deliveryObject = new DeliveryOwnClient.DeliveryClient("netTcpDeliveryService");
I want to use this without any configuration: 我想在没有任何配置的情况下使用它:
IDelivery deliveryObject = InProcessFactory.CreateInstance<DeliveryService.Delivery, IDelivery>();
But this throws an exception that "The ChannelDispatcher at http://localhost:8003/DeliveryService with contract "IMetadataExchange" can't open the IChannelListener. A registration already exists for Uri net.tcp://localhost:9003/DeliveryService" 但这引发了一个异常,“ http:// localhost:8003 / DeliveryService with contract”的ChannelDispatcher“IMetadataExchange”无法打开IChannelListener。对于Uri net.tcp:// localhost:9003 / DeliveryService已经存在注册
The implementation for CreateInstance looks like this: CreateInstance的实现如下所示:
ServiceHost host = new ServiceHost(typeof(S));
string address = "net.pipe://" + Environment.MachineName + "/" + Guid.NewGuid();
host.AddServiceEndpoint(typeof(I), Binding, address);
host.Open();
So I'm adding a net.pipe baseaddress and it fails because there is something running over net.tcp already. 所以我添加了一个net.pipe baseaddress,它失败了,因为net.tcp上已经有了一些东西。
** edit ** **编辑**
Figured out at least why this is happening. 至少弄清楚为什么会发生这种情况。
The service is configured in the app.config with two baseaddresses 该服务在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>
When the host is constructed 当主机构建时
ServiceHost host = new ServiceHost(typeof(S));
It find that section in the config file and automatically adds net.tcp and http base addresses. 它在配置文件中找到该部分并自动添加net.tcp和http基地址。 I add net.pipe, but that doesnt matter.
我添加net.pipe,但这没关系。 When the service is opened it finds that net.tcp is already running so it wont continue.
当服务打开时,它发现net.tcp已经在运行,所以它不会继续。
So I guess my question is changed into: Is it possible to construct a ServiceHost without having it read app.config? 所以我想我的问题变成了:是否有可能在没有读取app.config的情况下构建ServiceHost?
Jay managed to figure it out! 周杰伦设法搞清楚了! ServiceHost derives from ServiceHostBase and that class has a virtual function named ApplyConfiguration.
ServiceHost派生自ServiceHostBase,该类具有名为ApplyConfiguration的虚函数。 So I made a class which derives from ServiceHost and overrides ApplyConfiguration...and leave it empty.
所以我创建了一个派生自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()
{
}
}
Use it like this: 像这样使用它:
ServiceHost host = new ServiceHostNoConfig<S>(address);
host.AddServiceEndpoint(typeof(I), Binding, address);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.