C# 在WCF服务中传递用户定义的参数导致问题
我正在创建一个wcf自托管服务。我正在使用UriTemplate类自定义方法的URL。下面给出了代码片段C# 在WCF服务中传递用户定义的参数导致问题,c#,.net,wcf,C#,.net,Wcf,我正在创建一个wcf自托管服务。我正在使用UriTemplate类自定义方法的URL。下面给出了代码片段 public interface ISelfService { [WebInvoke(Method = "POST", UriTemplate = "ack/{errorcode}/{uniquefileid}")] [OperationContract] void Ack(ErrorCode errorcode, string un
public interface ISelfService
{
[WebInvoke(Method = "POST", UriTemplate = "ack/{errorcode}/{uniquefileid}")]
[OperationContract]
void Ack(ErrorCode errorcode, string uniquefileid);
[WebInvoke(Method = "POST", UriTemplate = "filechanged/{metainfo}")]
[OperationContract]
void FileChanged(MetaInformation metainfo);
}
每当我运行这个程序时,我都会遇到以下错误
合约“ISelfHostService”中的操作“FileChanged”具有查询
名为“metainfo”的变量,类型为“Natash.Common.MetaInformation”,
但是类型“Natash.Common.MetaInformation”不能通过
“QueryStringConverter”。UriTemplate查询值的变量必须
具有可由“QueryStringConverter”转换的类型
有人能告诉我为什么会这样吗
而且,我还没有对web.config文件进行任何修改。我需要在那里做些修改吗
元信息的定义如下
[DataContract]
public struct MetaInformation
{
[DataMember]
public string Author { get; set; }
[DataMember]
public string tags { get; set; }
[DataMember]
public string categories { get; set; }
[DataMember]
public string description { get; set; }
}
从您发布的消息中,听起来元信息类有两个定义(
Gettrix.Common.MetaInformation
&Natash.Common.MetaInformation
)
这可能是在WCF实例化服务时可以看到的。如果是这样,它可能会认为没有DataContract属性(可能是Natash.Common.MetaInformation
)的文件就是您要引用的文件,因此无法用于服务内的数据传输。试试这个
公共接口ISelfService{
[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = "/ack?errorcode={errorcode}&uniquefileid={uniquefileid}")]
void Ack(ErrorCode errorcode, string uniquefileid);
[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = "/filechanged")]
void FileChanged(MetaInformation metainfo);}
我认为您的类型是不可转换的。这是一个输入错误。只有一个名称空间是Natash.Common.MetaInformation。感谢您指出这一点。编辑了这个问题。我刚刚意识到问题所在,您不能在url中使用除基元类型以外的任何其他类型。要传递复杂类型,它必须包含在bo中您需要将url签名更改为“filechanged/”。WCF将从主体中提取数据(指定为JSON或XML),并为您实例化元信息类。