WCF SoapHeader在函数调用时没有请求参数

时间:2015-08-17 09:36:35

标签: c# wcf

我有一个WCF服务,它接收来自OperationContext.Current.RequestContext.RequestMessage的输入,而不是传入的任何参数。
我没有DataContract 我该如何实施SoapHeader

我服务的界面:

[ServiceContract(Namespace = Namespaces.Example)]
public interface IExample
{
    [OperationContract]
    string ExampleRequests();
}

服务:

[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall, Namespace = Namespaces.Example)]
public class Example: IExample
{
      public string ExampleRequests(){...}
}


1 个答案:

答案 0 :(得分:0)

我使用了https://wcfextras.codeplex.com/和SoapHeader属性来完成此任务。

更新界面:



    [ServiceContract(Namespace = Namespaces.Example)]
    public interface IExample
    {
        [SoapHeader("MyHeader", typeof(MyHeader), Direction = SoapHeaderDirection.In)]
        [OperationContract]
        string ExampleRequests();

    }


更新的服务:



    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall, Namespace = Namespaces.Example)]
    public class Example: IExample
    {

        public string ExampleRequests()
        {
        ...
        MyHeader soapHeader = SoapHeaderHelper.GetInputHeader("MyHeader");
        if (soapHeader == null || soapHeader.Username.IsNullOrEmpty() || !ValidateCredentials(soapHeader))
        {
        return "You do not have permission to access this service";
        }
        ...
        }

        private bool ValidateCredentials(MyHeader soapHeader)
        {
        if (soapHeader.Username == "admin" && soapHeader.Password == "123")
        {
        return true; 
        }
        return false;
        }

    }

MyHeader类:



    [DataContract(Namespace = Namespaces.Example)]
    public class MyHeader
    {
        [DataMember]
        public string Username { get; set; }
        [DataMember]
        public string Password { get; set; }

    }