首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法使用参数调用WebInvoke方法

无法使用参数调用WebInvoke方法
EN

Stack Overflow用户
提问于 2014-07-30 21:08:59
回答 1查看 522关注 0票数 0
代码语言:javascript
复制
[ServiceContract]
public interface IMyService
{
    [OperationContract]
    [WebGet(UriTemplate = "/v1/getCustomBodyTypes", ResponseFormat = WebMessageFormat.Json)]
    [JSONPBehavior(callback = "callback")]
    List<String> v1_GetCustomBodyType();

    [OperationContract]
    [WebInvoke(UriTemplate = "/v1/addCustomBodyType", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
    [JSONPBehavior(callback = "callback")]
    Response v1_AddCustomBodyType();
}

在IIS上实现接口并托管WCF服务之后,我可以从客户端调用上面的OperationContract。

但是,当像下面这样为它添加一个参数时,我可以调用v1_GetCustomBodyType,但在调用v1_AddCustomBodyType时遇到了以下错误:“基础连接已关闭:接收时出现意外错误”

代码语言:javascript
复制
[DataContract]
public class CustomBodyType
{
    [DataMember]
    public string Redbook { get; set; }
    [DataMember]
    public string CustomBodyStyle { get; set; }
}

[ServiceContract]
public interface IMyService
{
    [OperationContract]
    [WebGet(UriTemplate = "/v1/getCustomBodyTypes", ResponseFormat = WebMessageFormat.Json)]
    [JSONPBehavior(callback = "callback")]
    List<String> v1_GetCustomBodyTypes();

    [OperationContract]
    [WebInvoke(UriTemplate = "/v1/addCustomBodyType", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
    [JSONPBehavior(callback = "callback")]
    Response v1_AddCustomBodyType(CustomBodyType data);
}

我通过WebRequest调用服务

JSONPBehavior源码:

代码语言:javascript
复制
public class JSONPBehavior : Attribute, IOperationBehavior
{
    public string callback;
    #region IOperationBehavior Members
    public void AddBindingParameters(
      OperationDescription operationDescription, BindingParameterCollection bindingParameters
    )
    { return; }

    public void ApplyClientBehavior(OperationDescription operationDescription, ClientOperation clientOperation)
    {
        clientOperation.ParameterInspectors.Add(new Inspector(callback));
    }

    public void ApplyDispatchBehavior(OperationDescription operationDescription, DispatchOperation dispatchOperation)
    {
        dispatchOperation.ParameterInspectors.Add(new Inspector(callback));
    }

    public void Validate(OperationDescription operationDescription) { return; }

    #endregion

    //Parameter inspector
    class Inspector : IParameterInspector
    {
        string callback;
        public Inspector(string callback)
        {
            this.callback = callback;
        }

        public void AfterCall(string operationName, object[] outputs, object returnValue, object correlationState)
        {
        }

        public object BeforeCall(string operationName, object[] inputs)
        {
            string methodName = WebOperationContext.Current.IncomingRequest.UriTemplateMatch.QueryParameters[callback];
            if(methodName !=null)
            {                    
                //System.ServiceModel.OperationContext.Current.OutgoingMessageProperties["wrapper"] = inputs[0];
                JSONPMessageProperty property = new JSONPMessageProperty()
                {
                    MethodName = methodName
                };
                OperationContext.Current.OutgoingMessageProperties.Add(JSONPMessageProperty.Name, property);
            }
            return null;
        }
    }

}
EN

回答 1

Stack Overflow用户

发布于 2014-07-31 10:59:38

不确定为什么它无法将发布数据映射到输入参数。但是,我可以使用字符串参数作为发布数据。

代码语言:javascript
复制
[OperationContract]
[WebInvoke(UriTemplate = "/v1/addCustomBodyType", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
[JSONPBehavior(callback = "callback")]
Response v1_AddCustomBodyType(string data);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25037678

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档