簡體   English   中英

使用XML調用WCF服務

[英]Call WCF service with XML

我正在嘗試調用WCF服務。

但是,我將請求正文作為XML文檔。

因此,例如,代替這個

ListProductsRequest request = new ListProductsRequest();
request.Query = new RootQuery();
request.Query.Product = "milk";
request.Query.Group = "dairy";

ListProductsPortType client = new ListProductsPortTypeClient();
ListProductsResponse response = client.ListProducts(request);

我想這樣做:

String xml = "<Root xmlns=\"urn:ns\"><Query><Group>dairy</Group><Product>milk</Product></Query></Root>";
var request = // read in to XmlReader or XmlDocument or whatever
ListProductsPortType client = new ListProductsPortTypeClient();
var response = client.ListProducts(request);

有沒有一種方法可以使用生成的代理,而該代理具有為我處理數據層安全性和傳輸但不使用代理對象的優點?

謝謝,布萊希特

我認為您不能調用WCF服務並傳遞您想要的東西。

ListProducts方法僅接受ListProductsRequest對象。 因此,您必須創建這種對象。

String xml = "<Root xmlns=\"urn:ns\"><Query><Group>dairy</Group><Product>milk</Product></Query></Root>";
ListProductsRequest request = MappingObject(xml); 
ListProductsPortType client = new ListProductsPortTypeClient();
var response = client.ListProducts(request);

在Mapping方法中,您可以使用XML創建ListproductRequest。

我不知道是否還有其他方法可以做到這一點。

由於2GDev的評論,我到現在為止。 沒有正確處理異常或異常情況的代碼。

這樣,我可以使用生成的存根的終結點(並因此重復使用配置等)。

    public void CallWs()
    {
        WsdlRDListProductsPortTypeClient client = new WsdlRDListProductsPortTypeClient();

        String req = "<Root xmlns=\"urn:ns\"><Query><Group>TV</Group><Product>TV</Product></Query></Root>";

        CallWs(client.Endpoint, "ListProducts", GetRequestXml(req));
    }

    public XmlElement CallWs(ServiceEndpoint endpoint, String operation, XmlElement request)
    {
        String soapAction = GetSoapAction(endpoint, operation);

        IChannelFactory<IRequestChannel> factory = null;

        try
        {
            factory = endpoint.Binding.BuildChannelFactory<IRequestChannel>();
            factory.Open();
            IRequestChannel channel = null;

            try
            {
                channel = factory.CreateChannel(endpoint.Address);
                channel.Open();

                Message requestMsg = Message.CreateMessage(endpoint.Binding.MessageVersion, soapAction, request);
                Message response = channel.Request(requestMsg);
                return response.GetBody<XmlElement>();
            }
            finally
            {
                if (channel != null)
                    channel.Close();
            }
        }
        finally
        {
            if (factory != null)
                factory.Close();
        }
    }

    private String GetSoapAction(ServiceEndpoint endpoint, String operation)
    {

        foreach (OperationDescription opD in endpoint.Contract.Operations)
        {
            if (opD.Name == operation)
            {
                foreach (MessageDescription msgD in opD.Messages)
                    if (msgD.Direction == MessageDirection.Input)
                    {
                        return msgD.Action;
                    }
            }

        }

        return null;
    }

當我嘗試使用msdn http://msdn.microsoft.com/zh-cn/library/ms734712.aspx的基本ICalculator示例進行此操作時

使用SPNego進行保護,我必須對此稍作更改,因為那時我們需要一個IRequestSessionChannel而不是IRequestChannel。

    public XmlElement CallWs(ServiceEndpoint endpoint, String operation, XmlElement request)
    {
        String soapAction = GetSoapAction(endpoint, operation);

        IChannelFactory<IRequestSessionChannel> factory = null;

        try
        {


            factory = endpoint.Binding.BuildChannelFactory<IRequestSessionChannel>();
            factory.Open();
            IRequestSessionChannel channel = null;

            try
            {
                channel = factory.CreateChannel(endpoint.Address);
                channel.Open();

                Message requestMsg = Message.CreateMessage(endpoint.Binding.MessageVersion, soapAction, request);

                Message response = channel.Request(requestMsg);
                return response.GetBody<XmlElement>();
            }
            finally
            {
                if (channel != null)
                    channel.Close();
            }
        }
        finally
        {
            if (factory != null)
                factory.Close();
        }
    }

它確實進行了協商,並且似乎發送了一條消息,但是不幸的是,我現在收到以下錯誤消息:

No signature message parts were specified for messages with the 'http://Microsoft.ServiceModel.Samples/ICalculator/Add' action.

我想你可以用

ListProductsRequest request = (ListProductsRequest) new XmlSerializer(
    typeof(ListProductsRequest)).Deserialize();

創建相應的對象...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM