我试图通过使用fiddler发布字符串值来测试我的示例WCF服务,但是它返回了#34; 405错误"。以下是我的代码
[OperationContract]
[WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat= WebMessageFormat.Json, UriTemplate = "/json/process", BodyStyle = WebMessageBodyStyle.Bare)]
string ProcessOrderJSON(string request)
{
return "test json process";
}
下面是我的提琴手数据
URL: localhost:3173/json/process
User-Agent: Fiddler
content-type: application/json
Host: localhost:3173
RequestBody:
"testdata"
下面是我的配置文件。请帮我找出问题所在。提前致谢
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
<sectionGroup name="EmailSettings">
<section name="Default" type="System.Configuration.NameValueSectionHandler" />
<section name="Appraisal" type="System.Configuration.NameValueSectionHandler" />
<section name="CustomerService" type="System.Configuration.NameValueSectionHandler" />
<section name="TestAccounts" type="System.Configuration.NameValueSectionHandler" />
</sectionGroup>
</configSections>
<system.web>
<compilation debug="true" targetFramework="4.0" />
<customErrors mode="Off" />
</system.web>
<system.webServer>
<handlers>
<remove name="svc-Integrated-4.0" />
<add name="svc-Integrated-4.0" path="*" verb="*" type="System.ServiceModel.Activation.ServiceHttpHandlerFactory, System.ServiceModel.Activation, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" resourceType="Unspecified" requireAccess="Script" preCondition="integratedMode,runtimeVersionv4.0" />
</handlers>
<validation validateIntegratedModeConfiguration="false" />
<modules runAllManagedModulesForAllRequests="true">
<add name="UrlRoutingModule" type="System.Web.Routing.UrlRoutingModule, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
</modules>
<directoryBrowse enabled="true" />
</system.webServer>
<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
<behaviors>
<serviceBehaviors>
<behavior name="MEXGET">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service behaviorConfiguration="MEXGET" name="Appraisal.OrderService.WCF.OrderServiceMex">
</service>
</services>
<standardEndpoints>
<webHttpEndpoint>
<standardEndpoint name="" helpEnabled="true" automaticFormatSelectionEnabled="true" transferMode="Streamed" maxBufferPoolSize="2147483647" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647">
<readerQuotas maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxDepth="1000" maxNameTableCharCount="16384" />
</standardEndpoint>
</webHttpEndpoint>
</standardEndpoints>
</system.serviceModel>
<log4net>
<appender name="FileAppender" type="log4net.Appender.FileAppender">
<param name="File" value="OrderService.log" />
<!-- Example using environment variables in params -->
<!-- <param name="File" value="${TMP}\\ApplicationKit.log" /> -->
<param name="AppendToFile" value="true" />
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%d %-2p - %m%n" />
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<LevelMin value="INFO" />
<LevelMax value="INFO" />
</filter>
<filter type="log4net.Filter.DenyAllFilter" />
</appender>
<appender name="ErrorsFileAppender" type="log4net.Appender.FileAppender">
<param name="File" value="ErrorOrderService.log" />
<!-- Example using environment variables in params -->
<!-- <param name="File" value="${TMP}\\ApplicationKit.log" /> -->
<param name="AppendToFile" value="true" />
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-2p %c [%x] - %m%n" />
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="ERROR" />
<LevelMax value="FATAL" />
</filter>
<filter type="log4net.Filter.DenyAllFilter" />
</appender>
<root>
<level value="ALL" />
<appender-ref ref="FileAppender" />
<appender-ref ref="ErrorsFileAppender" />
</root>
</log4net>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Common.Logging" publicKeyToken="af08829b84f0328e" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-2.3.1.0" newVersion="2.3.1.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
答案 0 :(得分:0)
405 HTTP状态代码表示该服务不支持的请求方法。 Detail
您的服务方法使用WebInvoke
属性及其Method
属性设置为POST。
如果您使用GET方法来调用服务方法,则此状态代码将返回。
使用 POST 更改您的请求方法。
答案 1 :(得分:0)
创建一个简单的控制台应用程序并使用WebClient来访问WCF休息服务
private static void PlaceOrder()
{
string data = "your string";
WebClient webClient = new WebClient();
webClient.Headers["Content-type"] = "application/json";
webClient.Encoding = Encoding.UTF8;
webClient.UploadString("http://localhost:61090/OrderService.svc
/PlaceOrder", "POST", data);
}
答案 2 :(得分:0)
您在配置文件中的问题,需要显式添加webHttp端点,这里是最小配置文件的示例:
<system.serviceModel>
<services>
<service name="WcfServiceLibrary3.Service1">
<host>
<baseAddresses>
<add baseAddress = "http://localhost:8733/" />
</baseAddresses>
</host>
<endpoint address="web" binding="webHttpBinding" contract="WcfServiceLibrary3.IService1" behaviorConfiguration="webBehavior"/>
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior name="webBehavior">
<webHttp />
</behavior>
</endpointBehaviors>
</behaviors>
</system.serviceModel>
此案例中的网址为http://localhost:8733/web/json/process