wcf - 不允许 JSON WCF 方法

标签 wcf

我有像这样的契约(Contract)的 WCF

        [WebInvoke(Method = "POST",
ResponseFormat = WebMessageFormat.Json,
RequestFormat = WebMessageFormat.Json,
UriTemplate = "UpdateEncounterStatus/{BookingID}/{BookingStatus}")]
        public void UpdateEncounterStatus(string BookingID, string BookingStatus)

我用它来调用它

http://localhost:1185/PMAHost/Service.svc/UpdateEncounterStatus/141/sfsa

但它给出

web.config 是

<?xml version="1.0"?>
<configuration>
  <system.diagnostics>
    <sources>
      <source name="System.ServiceModel" switchValue="All" propagateActivity="true">
        <listeners>
          <add type="System.Diagnostics.DefaultTraceListener" name="Default">
            <filter type=""/>
          </add>
          <add name="xml">
            <filter type=""/>
          </add>
        </listeners>
      </source>
      <source name="System.ServiceModel.MessageLogging" switchValue="All">
        <listeners>
          <add type="System.Diagnostics.DefaultTraceListener" name="Default">
            <filter type=""/>
          </add>
          <add name="xml">
            <filter type=""/>
          </add>
        </listeners>
      </source>
      <source name="XMLService.dll" switchValue="Warning, ActivityTracing">
        <listeners>
          <add type="System.Diagnostics.DefaultTraceListener" name="Default">
            <filter type=""/>
          </add>
          <add name="xml">
            <filter type=""/>
          </add>
        </listeners>
      </source>
    </sources>
    <sharedListeners>
      <add initializeData="C:\XMLServiceTrace.svclog"
           type="System.Diagnostics.XmlWriterTraceListener, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
           name="xml"
           traceOutputOptions="LogicalOperationStack, DateTime, Timestamp, ProcessId, ThreadId, Callstack">
        <filter type=""/>
      </add>
    </sharedListeners>
    <trace autoflush="true"/>
  </system.diagnostics>
  <connectionStrings>

    <roleManager enabled="true" />
    <membership>
      <providers>
        <remove name="AspNetSqlMembershipProvider"/>
        <add name="AspNetSqlMembershipProvider"
             type="System.Web.Security.SqlMembershipProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
             connectionStringName="Login"
             enablePasswordRetrieval="false"
             enablePasswordReset="true"
             requiresQuestionAndAnswer="false"
             applicationName="/"
             requiresUniqueEmail="false"
             passwordFormat="Hashed"
             maxInvalidPasswordAttempts="5"
             minRequiredPasswordLength="1"
             minRequiredNonalphanumericCharacters="0"
             passwordAttemptWindow="10"
             passwordStrengthRegularExpression="" />
      </providers>
    </membership>

    <authentication mode="Windows"/>
    <customErrors mode="RemoteOnly"/>
    <compilation debug="true" strict="false" explicit="true" targetFramework="4.0" />
  </system.web>
  <system.serviceModel>
    <services>
      <service name="PMAService.PMA">
        <endpoint address="" binding="webHttpBinding" contract="PMAService.IPMA" behaviorConfiguration="web">
        </endpoint>
      </service>
    </services>
    <behaviors>
      <endpointBehaviors>
        <behavior name="web">
          <webHttp />
          <dataContractSerializer maxItemsInObjectGraph="2147483647"/>
        </behavior>
      </endpointBehaviors>
      <serviceBehaviors>
        <behavior>
          <dataContractSerializer maxItemsInObjectGraph="2147483647" />
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <bindings>
      <webHttpBinding>
        <binding name="defaultRest">
          <readerQuotas maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxDepth="64" maxNameTableCharCount="2147483647" />
          <security mode="None" />
        </binding>
      </webHttpBinding>
    </bindings>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
    <modules>
      <remove name="WebDAVModule" />
    </modules>
  </system.webServer>
</configuration>

最佳答案

UpdateEncounterStatus方法被标记为 POST 请求,当您尝试从浏览器地址栏发出请求时,您将发出 GET 请求,因此您将得到 405 Method Not Allowed error 。要解决此问题,您必须使用 WebGet 标记该方法。或WebInvoke(Method="GET")

更新:在您的情况下,您正在执行更新操作,因此根据 REST,您不应将其更改为 GET only POST。

关于wcf - 不允许 JSON WCF 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11012606/

相关文章:

c# - svcutil NOT 为 minOccurs=0 生成指定字段

c# - 启用 GZIP 以在客户端调用 WCF 服务

c# - 在字符串中查找字符串可以为空的单词(有时)

c# - 设计 SOA WCF Web 服务时的最佳实践是什么?

c# - 使用表单例份验证调用 WCF 服务时避免重定向

wcf - 序列化 FileStream 以进行传输

c# - 无法使用基本身份验证访问 WCF Web 服务

.net - WCF netTCPBinding - 传输加密是否足够?

c# - 如何将用户名/密码凭据从 php 客户端传递到自托管 wcf 服务?

c# - 如何更改 WCF 的默认配置?