尝试调用WCF webservice 4.0时获取返回类型无效错误 [英] Getting return type invalid error while trying to call WCF webservice 4.0

查看:888
本文介绍了尝试调用WCF webservice 4.0时获取返回类型无效错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写和调用WCF Web服务,下面是详细信息:



Web.config: b
$ b

 < add relativeAddress =FetchData.svcservice =WCF.Services.FetchData/> 

< service name =WCF.Services.FetchData>
< endpoint address =binding =webHttpBindingbindingConfiguration =name =FetchDatacontract =WCF.Services.FetchData/>
< / service>

FetchData类别(示例代码)

 使用System; 
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.ServiceModel.Activation;
使用System.Web;
using System.Xml;
使用Webservices.Services;
using Data = Webservices.Data;
使用System.ServiceModel.Web;
using System.IO;
使用System.Net;
using System.ServiceModel.Channels;
使用System.Web.UI;
using System.Text;


命名空间WCF.Services
{
[ServiceContract(Namespace =urn:WCF.Services.FetchData)]
public class FetchData
{
Data.GetConnect mConnect = new Data.GetConnect();
private Message RetrievePublishedData(String pub,int number)
{
String strOutput = String.Empty;
if(!String.IsNullOrEmpty(pub))
{
Boolean pubURLExists = mConnect.CheckPubUrlExists(pub);

if(!pubURLExists)
{
WebOperationContext.Current.OutgoingResponse.StatusCode = HttpStatusCode.NotFound;
return WebOperationContext.Current.CreateTextResponse(String.Format(Requested publication'{0}'is not available。,pub),MimeTypes.TextPlain,Encoding.UTF8);
}
using(StringWriter sw = new StringWriterEncoding())
{
using(HtmlTextWriter hw = new HtmlTextWriter(sw))
{
hw。 RenderBeginTag(HtmlTextWriterTag.Html);
XmlNode publishedData = mConnect.GetPublishedData(pub,number);
hw.RenderEndTag();
}
return WebOperationContext.Current.CreateTextResponse(sw.ToString(),MimeTypes.TextHTML,Encoding.UTF8);
}
}
return WebOperationContext.Current.CreateTextResponse(strOutput,MimeTypes.TextHTML,Encoding.UTF8);
}
[OperationContract]
[WebGet(UriTemplate =/ published / {number} / {* pub = default})]
public Message FetchPublished(String pub,int number)
{
return RetrievePublishedData(pub,number);
}
}
}

浏览Web服务,我得到以下错误:



Web服务URL - http:// localhost:8082 / FetchData.svc



错误:
无法加载操作FetchPublished,因为它有一个参数,返回类型为System.ServiceModel.Channels.Message的类型或具有不同类型的MessageContractAttribute和其他参数的类型。当使用System.ServiceModel.Channels.Message或类型与MessageContractAttribute时,该方法不能使用任何其他类型的参数。



/ strong>

 命名空间WCFWebServices 
{
[ServiceBehavior(AddressFilterMode = AddressFilterMode.Any)]
[ServiceContract(Namespace =urn:WCFWebServices.fetchPush)]
public class FetchData
{
[MessageContract]
public class RetrievePublishedDataInput
{
[MessageBodyMember]
public String pub;
[MessageBodyMember]
public String number;
}
private Message RetrievePublishedData(RetrievePublishedDataInput input)
{
String strOutput = String.Empty;
String pub = input.pub;
String number = input.number;
if(!String.IsNullOrEmpty(pub))
{
Boolean pubURLExists = true;

if(!pubURLExists)
{
WebOperationContext.Current.OutgoingResponse.StatusCode = HttpStatusCode.NotFound;
return WebOperationContext.Current.CreateTextResponse(String.Format(Requested publication'{0}'is not available。,pub),application / plain; charset = utf-8,Encoding.UTF8);
}
使用(StringWriter sw = new StringWriter())
{
使用(HtmlTextWriter hw = new HtmlTextWriter(sw))
{
hw。 RenderBeginTag(HtmlTextWriterTag.Html);

hw.RenderEndTag();
}
return WebOperationContext.Current.CreateTextResponse(sw.ToString(),application / html; charset = utf-8,Encoding.UTF8);
}
}
return WebOperationContext.Current.CreateTextResponse(strOutput,application / html; charset = utf-8,Encoding.UTF8);
}
[OperationContract]
[WebGet(UriTemplate =/ publishedData / {number} / {pub = default})]
public Message FetchPublished(RetrievePublishedDataInput input)
{
return RetrievePublishedData(input);
}
}
}


解决方案>

我相信提到的错误是相当自我解释。根据 MSDN ,使用消息类有其自己的限制:


您可以使用Message类作为操作的输入参数,操作的返回值或两者。如果在操作中的任何位置使用Message,则以下限制适用:




  • 此操作不能有任何out或ref参数。
  • 不能有多个输入参数。 如果参数存在,则必须是消息或消息合同类型

  • 返回类型必须为void,Message或消息合同类型。


如果是您的合同,则违反第二个限制。最简单的解决方法是创建适当的 MessageContract

  [MessageContract] 
public class RetrievePublishedDataInput
{
[MessageBodyMember] public string Pub;
[MessageBodyMember] public int Number;
}

私人消息RetrievePublishedData(RetrievePublishedDataInput input)
{
....
}


I am trying to write and call WCF web service, below are the details:

Web.config:

<add relativeAddress="FetchData.svc" service="WCF.Services.FetchData" />

<service name="WCF.Services.FetchData">
    <endpoint address="" binding="webHttpBinding" bindingConfiguration="" name="FetchData" contract="WCF.Services.FetchData" />
</service>

FetchData Class (Sample Code):

using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.Web;
using System.Xml;
using Webservices.Services;
using Data = Webservices.Data;
using System.ServiceModel.Web;
using System.IO;
using System.Net;
using System.ServiceModel.Channels;
using System.Web.UI;
using System.Text;


namespace WCF.Services
{    
    [ServiceContract(Namespace = "urn:WCF.Services.FetchData")]
    public class FetchData
    {
        Data.GetConnect mConnect = new Data.GetConnect();
        private Message RetrievePublishedData(String pub, int number)
        {
            String strOutput = String.Empty; 
            if (!String.IsNullOrEmpty(pub))
            {
                Boolean pubURLExists = mConnect.CheckPubUrlExists(pub);

                if (!pubURLExists)
                {
                    WebOperationContext.Current.OutgoingResponse.StatusCode = HttpStatusCode.NotFound;
                    return WebOperationContext.Current.CreateTextResponse(String.Format("Requested publication '{0}' is not available.", pub), MimeTypes.TextPlain, Encoding.UTF8);
                }
                using (StringWriter sw = new StringWriterEncoding())
                {
                    using (HtmlTextWriter hw = new HtmlTextWriter(sw))
                    {
                        hw.RenderBeginTag(HtmlTextWriterTag.Html);
                        XmlNode publishedData = mConnect.GetPublishedData(pub, number);
                        hw.RenderEndTag();
                    }
                    return WebOperationContext.Current.CreateTextResponse(sw.ToString(),MimeTypes.TextHTML, Encoding.UTF8);
                }
            }
            return WebOperationContext.Current.CreateTextResponse(strOutput, MimeTypes.TextHTML, Encoding.UTF8);
        }
        [OperationContract]
        [WebGet(UriTemplate = "/published/{number}/{*pub=default}")]
        public Message FetchPublished(String pub, int number)
        {
           return RetrievePublishedData(pub, number);
        }
    }
}

Now when I am trying to browse the web service, I am getting below error:

Web Service URL - http://localhost:8082/FetchData.svc

Error: The operation 'FetchPublished' could not be loaded because it has a parameter or return type of type System.ServiceModel.Channels.Message or a type that has MessageContractAttribute and other parameters of different types. When using System.ServiceModel.Channels.Message or types with MessageContractAttribute, the method must not use any other types of parameters.

Edit:

namespace WCFWebServices
{
    [ServiceBehavior(AddressFilterMode = AddressFilterMode.Any)]
     [ServiceContract(Namespace = "urn:WCFWebServices.fetchPush")]
        public class FetchData
        {
         [MessageContract]
         public class RetrievePublishedDataInput
         {
             [MessageBodyMember]
             public String pub;
             [MessageBodyMember]
             public String number;
         }
            private Message RetrievePublishedData(RetrievePublishedDataInput input)
            {
                String strOutput = String.Empty;
                String pub = input.pub;
                String number = input.number;
                if (!String.IsNullOrEmpty(pub))
                {
                    Boolean pubURLExists = true;

                    if (!pubURLExists)
                    {
                        WebOperationContext.Current.OutgoingResponse.StatusCode = HttpStatusCode.NotFound;
                        return WebOperationContext.Current.CreateTextResponse(String.Format("Requested publication '{0}' is not available.", pub), "application/plain; charset=utf-8", Encoding.UTF8);
                    }
                    using (StringWriter sw = new StringWriter())
                    {
                        using (HtmlTextWriter hw = new HtmlTextWriter(sw))
                        {
                            hw.RenderBeginTag(HtmlTextWriterTag.Html);

                            hw.RenderEndTag();
                        }
                        return WebOperationContext.Current.CreateTextResponse(sw.ToString(), "application/html; charset=utf-8", Encoding.UTF8);
                    }
                }
                return WebOperationContext.Current.CreateTextResponse(strOutput, "application/html; charset=utf-8", Encoding.UTF8);
            }
            [OperationContract]
            [WebGet(UriTemplate = "/publishedData/{number}/{pub=default}")]
            public Message FetchPublished(RetrievePublishedDataInput input)
            {
                return RetrievePublishedData(input);
            }
        }      
}

解决方案

I believe mentioned error is quite self-explaining. According to the MSDN, using Message class has its own restrictions:

You can use the Message class as an input parameter of an operation, the return value of an operation, or both. If Message is used anywhere in an operation, the following restrictions apply:

  • The operation cannot have any out or ref parameters.
  • There cannot be more than one input parameter. If the parameter is present, it must be either Message or a message contract type.
  • The return type must be either void, Message, or a message contract type.

In case of your contract, the second restriction is violated. The easiest workaround is to create proper MessageContract:

[MessageContract]
public class RetrievePublishedDataInput
{
  [MessageBodyMember] public string Pub;
  [MessageBodyMember] public int Number;
}

private Message RetrievePublishedData(RetrievePublishedDataInput input)
{
    ....
}

这篇关于尝试调用WCF webservice 4.0时获取返回类型无效错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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