コメントに基づいて追加したことを実際に行うことはできません。
ServiceHost host = new ServiceHost(typeof(Service1));
host.Open();
(Service1)host.MyProperty = "asd";
その時点では、クラスのインスタンスはService1
まだ作成されていないためです。また、サービスに対する新しいリクエストが到着したときにのみ作成されます。
1つの代替方法は、カスタムインスタンスプロバイダー(以下のコードに表示)を使用することです。このプロバイダーでは、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();
}
}