开发者

Check the InstanceContextMode used by a WCF service

Is there any way to check what InstanceContextMode is used by my WCF service?

Can I find/write this value in svclog file?

Thank y开发者_如何学Pythonou!


It's not logged on to the traces. But you can find that information during runtime (via the OperationContext, and log it somewhere yourself.

public class StackOverflow_7360920
{
    [ServiceContract]
    public interface ITest
    {
        [OperationContract]
        int Add(int x, int y);
    }
    //[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
    public class Service : ITest
    {
        public Service()
        {
            Console.WriteLine(OperationContext.Current.Host.Description.Behaviors.Find<ServiceBehaviorAttribute>().InstanceContextMode);
        }
        public int Add(int x, int y)
        {
            return x + y;
        }
    }
    static Binding GetBinding()
    {
        BasicHttpBinding result = new BasicHttpBinding();
        return result;
    }
    public static void Test()
    {
        string baseAddress = "http://" + Environment.MachineName + ":8000/Service";
        ServiceHost host = new ServiceHost(typeof(Service), new Uri(baseAddress));
        host.AddServiceEndpoint(typeof(ITest), GetBinding(), "");
        host.Open();
        Console.WriteLine("Host opened");

        ChannelFactory<ITest> factory = new ChannelFactory<ITest>(GetBinding(), new EndpointAddress(baseAddress));
        ITest proxy = factory.CreateChannel();

        Console.WriteLine(proxy.Add(3, 5));

        ((IClientChannel)proxy).Close();
        factory.Close();

        Console.Write("Press ENTER to close the host");
        Console.ReadLine();
        host.Close();
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜