您不能真正根据评论执行您添加的操作:
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();
}
}