Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 我已经实现了AfterReceiveRequest方法(来自IDispatchMessageInspector),并在其中验证请求是否有效?_C#_Wcf - Fatal编程技术网

C# 我已经实现了AfterReceiveRequest方法(来自IDispatchMessageInspector),并在其中验证请求是否有效?

C# 我已经实现了AfterReceiveRequest方法(来自IDispatchMessageInspector),并在其中验证请求是否有效?,c#,wcf,C#,Wcf,在调用服务之前的客户端中,我在发送到wcf服务之前向请求添加了一个令牌(guid) AfterReceiveRequest(ref System.ServiceModel.Channels.Message request, System.ServiceModel.IClientChannel channel, System.ServiceModel.InstanceContext instanceContext) 在这个方法中,我想验证请求头是否具有添加到客户机中的令牌(guid)。所以我在这

在调用服务之前的客户端中,我在发送到wcf服务之前向请求添加了一个令牌(guid)

AfterReceiveRequest(ref System.ServiceModel.Channels.Message request, System.ServiceModel.IClientChannel channel, System.ServiceModel.InstanceContext instanceContext)
在这个方法中,我想验证请求头是否具有添加到客户机中的令牌(guid)。所以我在这里使用了这部分代码:

Guid securityTokenId = request.Headers.GetHeader<Guid>("Token", "System");
Guid securityTokenId=request.Headers.GetHeader(“令牌”、“系统”);

如果在任何情况下,在标题中未找到此“令牌”,则my
securityTokenId
将返回
Guid.Empty
?或者它返回什么?

如果没有具有该名称/命名空间的头,则在对GetHeader的调用中会出现异常(
MessageHeaderException
)。如果不想处理异常,可以首先查看是否存在这样的头(使用
FindHeader
方法),然后如果它返回非负值,则获取相应的头

public class StackOverflow_8854137
{
    [ServiceContract]
    public interface ITest
    {
        [OperationContract]
        string Echo(string text);
    }
    public class Service : ITest
    {
        public string Echo(string text)
        {
            return text;
        }
    }
    class MyBehavior : IEndpointBehavior, IDispatchMessageInspector
    {
        public void AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
        {
        }

        public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
        {
        }

        public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
        {
            endpointDispatcher.DispatchRuntime.MessageInspectors.Add(this);
        }

        public void Validate(ServiceEndpoint endpoint)
        {
        }

        public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext)
        {
            try
            {
                Guid tokenId = request.Headers.GetHeader<Guid>("Token", "System");
                Console.WriteLine("Token: {0}", tokenId);
            }
            catch (Exception e)
            {
                Console.WriteLine("{0}: {1}", e.GetType().FullName, e.Message);
            }

            return null;
        }

        public void BeforeSendReply(ref Message reply, object correlationState)
        {
        }
    }
    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), new BasicHttpBinding(), "");
        endpoint.Behaviors.Add(new MyBehavior());
        host.Open();
        Console.WriteLine("Host opened");

        ChannelFactory<ITest> factory = new ChannelFactory<ITest>(new BasicHttpBinding(), new EndpointAddress(baseAddress));
        ITest proxy = factory.CreateChannel();
        Console.WriteLine(proxy.Echo("No token"));

        using (new OperationContextScope((IContextChannel)proxy))
        {
            OperationContext.Current.OutgoingMessageHeaders.Add(MessageHeader.CreateHeader("Token", "System", Guid.NewGuid()));
            Console.WriteLine(proxy.Echo("With token"));
        }

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

        Console.Write("Press ENTER to close the host");
        Console.ReadLine();
        host.Close();
    }
}
公共类StackOverflow_8854137
{
[服务合同]
公共接口测试
{
[经营合同]
字符串回显(字符串文本);
}
公共类服务:ITest
{
公共字符串回显(字符串文本)
{
返回文本;
}
}
类MyBehavior:IEndpointBehavior,IDispatchMessageInspector
{
public void AddBindingParameters(ServiceEndpoint端点、BindingParameterCollection bindingParameters)
{
}
public void ApplyClientBehavior(ServiceEndpoint端点、ClientRuntime ClientRuntime)
{
}
公共无效ApplyDispatchBehavior(ServiceEndpoint端点、EndpointDispatcher端点Dispatcher)
{
endpointDispatcher.DispatcheRuntime.MessageInspectors.Add(此);
}
公共void验证(ServiceEndpoint)
{
}
接收请求后的公共对象(ref消息请求、IClientChannel通道、InstanceContext InstanceContext)
{
尝试
{
Guid tokenId=request.Headers.GetHeader(“令牌”、“系统”);
WriteLine(“令牌:{0}”,令牌ID);
}
捕获(例外e)
{
WriteLine(“{0}:{1}”,e.GetType().FullName,e.Message);
}
返回null;
}
SendReply之前的公共无效(参考消息回复,对象关联状态)
{
}
}
公共静态无效测试()
{
string baseAddress=“http://“+Environment.MachineName+”:8000/服务”;
ServiceHost主机=新ServiceHost(类型(服务),新Uri(基地址));
ServiceEndpoint=host.AddServiceEndpoint(typeof(ITest),新的BasicHttpBinding(),“”);
添加(新的MyBehavior());
host.Open();
Console.WriteLine(“主机已打开”);
ChannelFactory工厂=新的ChannelFactory(新的BasicHttpBinding(),新的EndpointAddress(baseAddress));
ITest proxy=factory.CreateChannel();
Console.WriteLine(proxy.Echo(“无令牌”);
使用(新OperationContextScope((IContextChannel)代理))
{
OperationContext.Current.OutgoingMessageHeaders.Add(MessageHeader.CreateHeader(“令牌”、“系统”、Guid.NewGuid());
Console.WriteLine(proxy.Echo(“带令牌”);
}
((IClientChannel)代理).Close();
工厂关闭();
控制台。写入(“按ENTER键关闭主机”);
Console.ReadLine();
host.Close();
}
}

如果没有具有该名称/命名空间的头,则在对GetHeader的调用中会出现异常(
MessageHeaderException
)。如果不想处理异常,可以首先查看是否存在这样的头(使用
FindHeader
方法),然后如果它返回非负值,则获取相应的头

public class StackOverflow_8854137
{
    [ServiceContract]
    public interface ITest
    {
        [OperationContract]
        string Echo(string text);
    }
    public class Service : ITest
    {
        public string Echo(string text)
        {
            return text;
        }
    }
    class MyBehavior : IEndpointBehavior, IDispatchMessageInspector
    {
        public void AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
        {
        }

        public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
        {
        }

        public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
        {
            endpointDispatcher.DispatchRuntime.MessageInspectors.Add(this);
        }

        public void Validate(ServiceEndpoint endpoint)
        {
        }

        public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext)
        {
            try
            {
                Guid tokenId = request.Headers.GetHeader<Guid>("Token", "System");
                Console.WriteLine("Token: {0}", tokenId);
            }
            catch (Exception e)
            {
                Console.WriteLine("{0}: {1}", e.GetType().FullName, e.Message);
            }

            return null;
        }

        public void BeforeSendReply(ref Message reply, object correlationState)
        {
        }
    }
    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), new BasicHttpBinding(), "");
        endpoint.Behaviors.Add(new MyBehavior());
        host.Open();
        Console.WriteLine("Host opened");

        ChannelFactory<ITest> factory = new ChannelFactory<ITest>(new BasicHttpBinding(), new EndpointAddress(baseAddress));
        ITest proxy = factory.CreateChannel();
        Console.WriteLine(proxy.Echo("No token"));

        using (new OperationContextScope((IContextChannel)proxy))
        {
            OperationContext.Current.OutgoingMessageHeaders.Add(MessageHeader.CreateHeader("Token", "System", Guid.NewGuid()));
            Console.WriteLine(proxy.Echo("With token"));
        }

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

        Console.Write("Press ENTER to close the host");
        Console.ReadLine();
        host.Close();
    }
}
公共类StackOverflow_8854137
{
[服务合同]
公共接口测试
{
[经营合同]
字符串回显(字符串文本);
}
公共类服务:ITest
{
公共字符串回显(字符串文本)
{
返回文本;
}
}
类MyBehavior:IEndpointBehavior,IDispatchMessageInspector
{
public void AddBindingParameters(ServiceEndpoint端点、BindingParameterCollection bindingParameters)
{
}
public void ApplyClientBehavior(ServiceEndpoint端点、ClientRuntime ClientRuntime)
{
}
公共无效ApplyDispatchBehavior(ServiceEndpoint端点、EndpointDispatcher端点Dispatcher)
{
endpointDispatcher.DispatcheRuntime.MessageInspectors.Add(此);
}
公共void验证(ServiceEndpoint)
{
}
接收请求后的公共对象(ref消息请求、IClientChannel通道、InstanceContext InstanceContext)
{
尝试
{
Guid tokenId=request.Headers.GetHeader(“令牌”、“系统”);
WriteLine(“令牌:{0}”,令牌ID);
}
捕获(例外e)
{
WriteLine(“{0}:{1}”,e.GetType().FullName,e.Message);
}
返回null;
}
SendReply之前的公共无效(参考消息回复,对象关联状态)
{
}
}
公共静态无效测试()
{
string baseAddress=“http://“+Environment.MachineName+”:8000/服务”;
ServiceHost主机=新ServiceHost(类型(服务),新Uri(基地址));
ServiceEndpoint=host.AddServiceEndpoint(typeof(ITest),新的BasicHttpBinding(),“”);
添加(新的MyBehavior());
host.Open();
Console.WriteLine(“主机已打开”);
ChannelFactory工厂=新的ChannelFactory(新的BasicHttpBinding(),新的EndpointAddress(baseAddress));
ITest proxy=factory.CreateChannel();
Console.WriteLine(proxy.Echo(“No t