如何获取/设置从自承载 WCF 服务到应用程序其余部分的引用
本文关键字:应用程序 服务 WCF 余部 引用 获取 何获取 设置 | 更新日期: 2023-09-27 18:36:25
假设我有一个Windows窗体应用程序,该应用程序应该通过.net远程控制或受到远程位置的影响。
据我用谷歌搜索,在该应用程序中托管 WCF 服务将是要走的路。我已成功将 WCF 服务添加到应用程序,并且可以使用
ServiceHost host = new ServiceHost(typeof(Service1));
host.Open();
从正在运行的应用程序的其余部分获取对类的引用的好方法是什么?
我想只有这两种方法:
- 使用静态属性从服务方法中调用其他方法/设置属性。
- 是否分配对服务类的引用?(如何为服务主机中托管的服务分配值?
什么被认为是一种好的做法,或者更确切地说,过去哪种方式对您有用?
根据评论
进行更新我想我正在寻找这样的东西:
ServiceHost host = new ServiceHost(typeof(Service1));
host.Open();
(Service1)host.MyProperty = "asd";
我似乎找不到如何将服务主机(或其属性)强制转换为 Service1 的实例。这可能会解决我所有的问题;)
你不能真正做你根据评论添加的内容:
ServiceHost host = new ServiceHost(typeof(Service1));
host.Open();
(Service1)host.MyProperty = "asd";
因为此时尚未创建类Service1
的实例。并且,仅当服务的新请求到达时,才会创建它们。
一种替代方法是使用自定义实例提供程序(如下面的代码所示),在 WCF 运行时使用服务实例之前,您具有对服务实例的引用。您可以在 http://blogs.msdn.com/b/carlosfigueira/archive/2011/05/31/wcf-extensibility-iinstanceprovider.aspx 阅读有关实例提供程序的更多信息。
public class StackOverflow_10932251
{
[ServiceContract]
public interface ITest
{
[OperationContract]
string Echo(string text);
}
public class Service : ITest
{
public string MyProperty { get; set; }
public string Echo(string text)
{
Console.WriteLine("Inside Service.Echo, MyProperty = {0}", this.MyProperty);
return text;
}
}
static Binding GetBinding()
{
var result = new WSHttpBinding(SecurityMode.None);
return result;
}
public class MyInstanceProvider : IEndpointBehavior, IInstanceProvider
{
string propertyValue;
public MyInstanceProvider(string propertyValue)
{
this.propertyValue = propertyValue;
}
public void AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
{
}
public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
{
}
public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
{
endpointDispatcher.DispatchRuntime.InstanceProvider = this;
}
public void Validate(ServiceEndpoint endpoint)
{
}
public object GetInstance(InstanceContext instanceContext, Message message)
{
return new Service { MyProperty = this.propertyValue };
}
public object GetInstance(InstanceContext instanceContext)
{
return new Service { MyProperty = this.propertyValue };
}
public void ReleaseInstance(InstanceContext instanceContext, object instance)
{
}
}
public static void Test()
{
string baseAddress = "http://" + Environment.MachineName + ":8000/Service";
ServiceHost host = new ServiceHost(typeof(Service), new Uri(baseAddress));
ServiceEndpoint endpoint = host.AddServiceEndpoint(typeof(ITest), GetBinding(), "");
endpoint.Behaviors.Add(new MyInstanceProvider("asd"));
host.Open();
Console.WriteLine("Host opened");
ChannelFactory<ITest> factory = new ChannelFactory<ITest>(GetBinding(), new EndpointAddress(baseAddress));
ITest proxy = factory.CreateChannel();
Console.WriteLine(proxy.Echo("Hello"));
((IClientChannel)proxy).Close();
factory.Close();
Console.Write("Press ENTER to close the host");
Console.ReadLine();
host.Close();
}
}
创建ServiceHost
并调用 Open
时,服务开始侦听请求。默认行为(尽管您可以更改此行为)是仅在处理客户端请求时创建Service1
实例。换句话说,只要有客户端请求,ServiceHost
就会创建一个Service1
实例,然后调用其适当的方法(客户端调用的任何服务操作)来处理请求。因此,您不会尝试从ServiceHost
中检索(当然也不能强制转换)Service1
实例(除非有客户端请求,否则没有Service1
实例)。
如果您提供一个示例来说明为什么您需要将ServiceHost
转换为Service1
,我们也许能够提供替代方法。