无法使WCF WebCannelFactory在BeforeSendRequest工作

时间:2016-05-18 00:58:38

标签: wcf

第一个代码块返回一个好的请求。第二个返回一个错误。请指教。

第一个代码块返回有关客户的信息,它使用OAuth2。第二个块使用拦截器。问题是当我使用WebChannelFactory并在trace中查看我的请求时,它会显示(与一个好的请求相比)所有内容(请求的主体)都放入了消息块中。在一个好的请求中,我的请求的主体被放入肥皂消息中。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.Text;
using System.Threading.Tasks;

namespace Ahcccs.Isd.Aes.Breaz.Core.WCF.SOAP
{
    public class ConsumeGetVendorLookup
    {
        public void Consume(string wso2token)
        {
            Header header = new Header();
            header.Type = "?";
            header.ReturnCode = "?";
            header.Requestor = "?";
            header.Recipient = "?";
            header.Date = "?";
            Requestor requestor = new Requestor();
            requestor.UserID = "";
            requestor.Password = "";

            header.Subject = requestor;

            Payload payload = new Payload();
            GetVendorCustomerIn getVendorCustomer = new GetVendorCustomerIn();
            getVendorCustomer.IncludeGeneralInfo = true;
            getVendorCustomer.IncludeHeadquarters = true;
            getVendorCustomer.IncludePrenote_EFT = true;
            getVendorCustomer.IncludePrenote_EFTSpecified = true;
            getVendorCustomer.Vendor_Customer = "{our customer number}";

            payload.GetVendorCustomerIn = getVendorCustomer;

            AdvMessage advMessage = new AdvMessage();
            advMessage.Header = header;
            advMessage.Payload = payload;

            GetVendorCustomer customer = new GetVendorCustomer();
            customer.AdvMessage = advMessage;

            VendorServicesClient wcfClient = new VendorServicesClient();
            var s = wcfClient.State;
            wcfClient.Open();

            using (new OperationContextScope(wcfClient.InnerChannel))
            {
                WebProxy wproxy = new WebProxy(new Uri("http://{our proxy server and port}"), true);
                wproxy.BypassProxyOnLocal = true;
                wproxy.UseDefaultCredentials = true;

                WebRequest.DefaultWebProxy = wproxy;

                // Add a HTTP Header to an outgoing request    
                HttpRequestMessageProperty requestMessage = new HttpRequestMessageProperty();
                requestMessage.Headers["Authorization"] = " Bearer " + wso2token;

                OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name] = requestMessage;

                var result = wcfClient.getVendorCustomer(customer);
            }
        }
    }
}

这一个返回错误:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Description;
using System.ServiceModel.Dispatcher;
using System.ServiceModel.Web;
using System.Text;
using System.Threading.Tasks;

namespace Ahcccs.Isd.Aes.Breaz.Core.WCF.SOAP
{
    public class ConsumeGetVendorLookup
    {
        public void Consume(string wso2token)
        {
            Header header = new Header();
            header.Type = "?";
            header.ReturnCode = "?";
            header.Requestor = "?";
            header.Recipient = "?";
            header.Date = "?";
            Requestor requestor = new Requestor();
            requestor.UserID = "";
            requestor.Password = "";

            header.Subject = requestor;

            Payload payload = new Payload();
            GetVendorCustomerIn getVendorCustomer = new GetVendorCustomerIn();
            getVendorCustomer.IncludeGeneralInfo = true;
            getVendorCustomer.IncludeHeadquarters = true;
            getVendorCustomer.IncludePrenote_EFT = true;
            getVendorCustomer.IncludePrenote_EFTSpecified = true;
            getVendorCustomer.Vendor_Customer = "{our customer number}";

            payload.GetVendorCustomerIn = getVendorCustomer;

            AdvMessage advMessage = new AdvMessage();
            advMessage.Header = header;
            advMessage.Payload = payload;

            GetVendorCustomer customer = new GetVendorCustomer();
            customer.AdvMessage = advMessage;
            getVendorCustomerRequest customerReq = new getVendorCustomerRequest();
            customerReq.getVendorCustomer = customer;

            //VendorServicesClient wcfClient = new VendorServicesClient();
            //var s = wcfClient.State;
            //wcfClient.Open();

            var wcfClient = new WebChannelFactory<VendorServicesChannel>(
                new Uri("{the target uri}"));
            //var s = wcfClient.State;
            //wcfClient.Open();
            wcfClient.Endpoint.EndpointBehaviors.Add(new AuthenticationHeaderBehavior("txtUser", "txtPass", wso2token));

            //using (new OperationContextScope(wcfClient.InnerChannel))
            //{
            //    WebProxy wproxy = new WebProxy(new Uri("our proxy server and port"), true);
            //    wproxy.BypassProxyOnLocal = true;
            //    wproxy.UseDefaultCredentials = true;

            //    WebRequest.DefaultWebProxy = wproxy;

            //    // Add a HTTP Header to an outgoing request    
            //    HttpRequestMessageProperty requestMessage = new HttpRequestMessageProperty();
            //    requestMessage.Headers["Authorization"] = " Bearer " + wso2token;

            //    OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name] = requestMessage;

            //    var result = wcfClient.getVendorCustomer(customer);
            //}

            var proxy = wcfClient.CreateChannel();
            using ((IDisposable)proxy)
            using (OperationContextScope c = new OperationContextScope((IContextChannel)proxy))
            {
                //WebProxy wproxy = new WebProxy(new Uri("our proxy server and port"), true);
                //wproxy.BypassProxyOnLocal = true;
                //wproxy.UseDefaultCredentials = true;
                //WebRequest.DefaultWebProxy = wproxy;
                //Add a HTTP Header to an outgoing request    
                //HttpRequestMessageProperty requestMessage = new HttpRequestMessageProperty();
                //requestMessage.Headers["Authorization"] = " Bearer " + wso2token;

                //OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name] = requestMessage;

                var result = proxy.getVendorCustomer(customerReq);
            }
        }
    }

    public class AuthenticationHeader : IClientMessageInspector
    {
        #region Implementation of IClientMessageInspector

        string itsUser;
        string itsPass;
        string itsToken;

        public AuthenticationHeader(string user, string pass, string token)
        {
            itsUser = user;
            itsPass = pass;
            itsToken = token;
        }

        public object BeforeSendRequest(ref Message request,
            IClientChannel channel)
        {
            //HttpRequestMessageProperty hrmp = request.Properties["httpRequest"] as HttpRequestMessageProperty;
            //string encoded = System.Convert.ToBase64String(System.Text.Encoding.GetEncoding("ISO-8859-1").GetBytes(itsUser + ":" + itsPass));
            //hrmp.Headers.Add("Authorization", "Basic " + encoded);
            //return request;

            //HttpRequestMessageProperty hrmp = request.Properties["httpRequest"] as HttpRequestMessageProperty;
            //hrmp.Headers.Add("Authorization", " Bearer " + itsToken);

            //HttpRequestMessageProperty requestMessage = new HttpRequestMessageProperty();
            //requestMessage.Headers["Authorization"] = " Bearer " + itsToken;

            //OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name] = requestMessage;

            //return request;

            WebProxy wproxy = new WebProxy(new Uri("our proxy server and port"), true);
            wproxy.BypassProxyOnLocal = true;
            wproxy.UseDefaultCredentials = true;
            WebRequest.DefaultWebProxy = wproxy;

            //// Add a HTTP Header to an outgoing request    
            HttpRequestMessageProperty hrmp = request.Properties["httpRequest"] as HttpRequestMessageProperty;
            hrmp.Headers["Authorization"] = " Bearer " + itsToken;

            return request;
        }

        public void AfterReceiveReply(ref Message reply, object correlationState)
        {
            //Console.WriteLine("Received the following reply: '{0}'", reply.ToString());
        }
        #endregion
    }

    public class AuthenticationHeaderBehavior : IEndpointBehavior
    {
        #region Implementation of IEndpointBehavior

        readonly string itsUser;
        readonly string itsPass;
        readonly string itsToken;

        public AuthenticationHeaderBehavior(string user, string pass, string token)
            : base()
        {
            itsUser = user;
            itsPass = pass;
            itsToken = token;
        }

        public void Validate(ServiceEndpoint endpoint) { }

        public void AddBindingParameters(ServiceEndpoint endpoint,
            BindingParameterCollection bindingParameters) { }

        public void ApplyDispatchBehavior(ServiceEndpoint endpoint,
            EndpointDispatcher endpointDispatcher) { }

        public void ApplyClientBehavior(ServiceEndpoint endpoint,
            ClientRuntime clientRuntime)
        {
            clientRuntime.MessageInspectors.Add(new AuthenticationHeader(itsUser, itsPass, itsToken));
        }

        #endregion
    }
}

1 个答案:

答案 0 :(得分:0)

我认为答案是我忘记了我的频道绑定