【问题标题】:Trying to get basic Http URL with querystring parameters processed in WCF service尝试使用 WCF 服务中处理的查询字符串参数获取基本 Http URL
【发布时间】:2010-08-30 15:43:55
【问题描述】:

我正在尝试处理由我的服务器通过 WCF (.Net 3.5 SP1) 服务处理的第三方服务器 (Paypal) 的帖子。我只需要使用查询字符串获取和处理值。这听起来非常容易,但在一个周末之后,我仍然很难过。任何帮助将不胜感激。

将以下 URL 从我的浏览器传递到服务会导致 AddressFilter 不匹配错误(如下)。

http://localhost:9409/PPInfo.svc/ReadResponse?RESULT=0&AUTHCODE=10001&RESPMSG=APROVED&PNREF=12345

生产

<Fault xmlns="http://schemas.microsoft.com/ws/2005/05/envelope/none">
- <Code>
  <Value>Sender</Value> 
- <Subcode>
  <Value xmlns:a="http://schemas.microsoft.com/ws/2005/05/addressing/none">a:DestinationUnreachable</Value> 
  </Subcode>
  </Code>
- <Reason>
  <Text xml:lang="en-US">The message with To 'http://localhost:9409/PPInfo.svc/ReadResponse?RESULT=0&AUTHCODE=10001&RESPMSG=APROVED&PNREF=12345' cannot be processed at the receiver, due to an AddressFilter mismatch at the EndpointDispatcher. Check that the sender and receiver's EndpointAddresses agree.</Text> 
  </Reason>
  </Fault>

// web.config
 <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior name="ASEEESPrivate.PPInfoBehavior">
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <services>
      <service behaviorConfiguration="ASEEESPrivate.PPInfoBehavior" name="ASEEESPrivate.PPInfo">
        <endpoint address="" binding="webHttpBinding" contract="ASEEESPrivate.IPPInfo">
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
      </service>
    </services>
  </system.serviceModel>
</configuration>

=================================

[ServiceContract]
public interface IPPInfo
{
    // expecting RESULT = 0 and RESPMSG = APPROVED
    [OperationContract] 
    [WebGet(UriTemplate = "Presponse?RESULT={result}&AUTHCODE={authcode}&RESPMSG={respmsg}&AVSDATA={avsdata}&PNREF={pnref}", 
        BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Xml)]
    void ReadResponse();
}

==========================================

// write parameters of query string to tlkpOnlineMessage table
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall, AddressFilterMode=AddressFilterMode.Any)]
    public class PPInfo : IPPInfo
    {

        public void ReadResponse()
        {
            var qString = HttpContext.Current.Request.QueryString;

            var ctx =
                new MembershipEntities();

            var log = new tlkpOnlineMessage();

            foreach (string s in qString)
            {
                log.LoginPageMsg = s;
                ctx.AddTotlkpOnlineMessages(log);
            }

            ctx.SaveChanges();
        }
    }

【问题讨论】:

  • 您的UriTemplate 设置为Presponse?...。这是一个错字吗?应该是ReadResponse

标签: wcf query-string


【解决方案1】:

除了代码中的错字之外,我相信您还有两个很容易纠正的问题:

1) 对于 RESTful 服务,您应该定义端点行为并启用 webHttp 行为。您可以通过将&lt;endpointBehavior&gt; 添加到您的 web.config 来做到这一点:

<behaviors>
  <serviceBehaviors>
    <behavior name="ASEEESPrivate.PPInfoBehavior">
      <serviceMetadata httpGetEnabled="true" />
      <serviceDebug includeExceptionDetailInFaults="true" />
    </behavior>
  </serviceBehaviors>
  <endpointBehaviors>
    <behavior name="ASEEESPrivate.PPInfoEndpointBehavior">
      <webHttp />
    </behavior>
  </endpointBehaviors>
</behaviors>

然后将此端点​​行为添加到您的服务定义中:

  <service behaviorConfiguration="ASEEESPrivate.PPInfoBehavior" name="WcfRestService1.PPInfo">
    <endpoint address="" binding="webHttpBinding" contract="WcfRestService1.IPPInfo"
              behaviorConfiguration="ASEEESPrivate.PPInfoEndpointBehavior">
      <identity>
        <dns value="localhost" />
      </identity>
    </endpoint>
    <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
  </service>

2) 其次,您使用占位符(结果、验证码等)为您的服务定义 UriTemplate,但您没有在界面中为它们定义参数。如果您要使用占位符为您的服务定义定义一个UriTemplate,那么您需要让您的服务相应地定义这些参数。你可以这样做:

[ServiceContract]
public interface IPPInfo
{
    // expecting RESULT = 0 and RESPMSG = APPROVED
    [OperationContract]
    [WebGet(UriTemplate = "ReadResponse?RESULT={result}&AUTHCODE={authcode}&RESPMSG={respmsg}&AVSDATA={avsdata}&PNREF={pnref}",
        BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Xml)]
    void ReadResponse(string result, string authcode, string respmsg, string avsdata, string pnref);
}


[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall, AddressFilterMode = AddressFilterMode.Any)]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class PPInfo : IPPInfo
{

    public void ReadResponse(string result, string authcode, string respmsg, string avsdata, string pnref)
    {
      ...
    }
}

通过使用UriTemplate,您无需提取查询字符串参数;或者,如果您想从查询字符串中提取参数,请不要定义 UriTemplate

完成这两项更改后,我就可以让服务在我的机器上本地运行。

我希望这会有所帮助!

【讨论】:

    猜你喜欢
    • 2020-05-26
    • 1970-01-01
    • 2015-08-14
    • 1970-01-01
    • 1970-01-01
    • 2017-07-20
    • 1970-01-01
    • 2013-03-31
    • 2012-08-28
    相关资源
    最近更新 更多