检查 WCF 服务使用的 InstanceContextMode [英] Check the InstanceContextMode used by a WCF service

查看:20
本文介绍了检查 WCF 服务使用的 InstanceContextMode的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么方法可以检查我的 WCF 服务使用的是什么 InstanceContextMode?

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

谢谢!

解决方案

它没有登录到跟踪.但是您可以在运行时找到该信息(通过 OperationContext,并自己将其记录在某个地方.

公共类 StackOverflow_7360920{[服务合约]公共接口 ITest{【经营合同】int Add(int x, int y);}//[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]公共类服务:ITest{公共服务(){Console.WriteLine(OperationContext.Current.Host.Description.Behaviors.Find().InstanceContextMode);}public int Add(int x, int y){返回 x + y;}}静态绑定 GetBinding(){BasicHttpBinding 结果 = new BasicHttpBinding();返回结果;}公共静态无效测试(){string baseAddress = "http://" + Environment.MachineName + ":8000/Service";ServiceHost host = new ServiceHost(typeof(Service), new Uri(baseAddress));host.AddServiceEndpoint(typeof(ITest), GetBinding(), "");主机.Open();Console.WriteLine("主机打开");ChannelFactory<ITest>factory = new ChannelFactory(GetBinding(), new EndpointAddress(baseAddress));ITest 代理 = factory.CreateChannel();Console.WriteLine(proxy.Add(3, 5));((IClientChannel)proxy).Close();工厂关闭();Console.Write("按回车键关闭主机");Console.ReadLine();主机.关闭();}}

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

Can I find/write this value in svclog file?

Thank you!

解决方案

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();
    }
}

这篇关于检查 WCF 服务使用的 InstanceContextMode的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆