检查WCF服务使用的InstanceContextMode

ini*_*iki 1 wcf

有没有办法检查我的WCF服务使用什么InstanceContextMode?

我可以在svclog文件中找到/写入这个值吗?

谢谢!

car*_*ira 7

它没有登录到跟踪.但是你可以在运行时找到这些信息(通过OperationContext,并在自己的某个地方登录).

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();
    }
}
Run Code Online (Sandbox Code Playgroud)