我有一个实现现有应用程序插件的类.
我也将该类暴露为WCF服务.那部分到目前为止是有效的.我遇到的问题是我插入的应用程序创建了我想要使用的类的实例.
有没有办法将现有的类实例传递给WCF服务主机,以作为服务端点公开?
我知道(或可以弄清楚)如何制作WCF服务的单例实例,但这仍然无法帮助我.据我所知,单例实例仍将由WCF创建和提供.
我已经想到了其他的方法,但如果可以使用的话我宁愿拿这个.
一些代码.这是我的插件的构造函数:
// Setup the service host
var baseAddress = new Uri("http://localhost:8080/MyService/");
this.serviceHost = new ServiceHost(this.GetType(), baseAddress);
// Add our service endpoint
// Todo: Is there somewhere around here that I can provide an instance?
// Maybe in behavior somewhere?
this.serviceHost.AddServiceEndpoint(
typeof(ITheInterfaceMyClassDerivesFrom),
new BasicHttpBinding(),
""
);
// Add metadata exchange (so we see something when we go to that URL)
var serviceMetadataBehavior = this.serviceHost.Description.Behaviors
.Find<ServiceMetadataBehavior>();
if (serviceMetadataBehavior == null)
this.serviceHost.Description.Behaviors.Add(new ServiceMetadataBehavior());
this.serviceHost.AddServiceEndpoint(
typeof(IMetadataExchange),
new CustomBinding(new HttpTransportBindingElement()),
"MEX"
);
Run Code Online (Sandbox Code Playgroud)
这是在插件的OnStartedUp方法中(由我插入的应用程序调用):
serviceHost.Open();
Run Code Online (Sandbox Code Playgroud)
ServiceHost如果要执行此操作,则需要使用其他构造函数- 请参阅http://msdn.microsoft.com/en-us/library/ms585487.aspx上的MSDN文档.
public ServiceHost(
Object singletonInstance,
params Uri[] baseAddresses
)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1193 次 |
| 最近记录: |