Web调用POST“不允许使用方法";在Wcf中

编程入门 行业动态 更新时间:2024-10-22 23:39:17
本文介绍了Web调用POST“不允许使用方法";在Wcf中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我的Rest wcf服务如下...

my Rest wcf service as below...

[ServiceContract] public interface IWinPhoneService { [OperationContract] [WebInvoke(RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, UriTemplate = "getkpimeasuredata", Method = "POST", BodyStyle = WebMessageBodyStyle.Wrapped)] List<MeasureData> GetKpiMeasureData(DomainData data); [OperationContract] [WebGet(UriTemplate = "getdata/{value}", ResponseFormat = WebMessageFormat.Json)] string GetData(string value); // TODO: Add your service operations here } [DataContract] public class DomainData { [DataMember] public int? KPIId { get; set ; } [DataMember] public int? ScorecardId { get; set; } [DataMember] public short? CumulativeMonth { get; set; } [DataMember] public int? EngineeringOrgId { get; set; } [DataMember] public int? BusinessOrgId { get; set; } [DataMember] public int? DataValuetypeId { get; set; } }

当我使用Restsharp如下使用这项服务时

and when I consume this service using Restsharp as below

string URL = "<servername>:8085/WinPhoneService.svc"; RestClient client = new RestClient(URL); RestRequest request = new RestRequest("getkpimeasuredata",Method.POST); DomainData data = new DomainData(); data.KPIId = 1006; data.ScorecardId = 3; data.EngineeringOrgId = 11; data.DataValuetypeId = 1; data.CumulativeMonth = 463; data.BusinessOrgId = 1; string json = Newtonsoft.Json.JsonConvert.SerializeObject(data); json = "{\"data\" : " + json + "}"; request.AddParameter("application/json; charset=utf-8", json, ParameterType.RequestBody); request.RequestFormat = DataFormat.Json; client.ExecuteAsync(request, response => { if (response.StatusCode == HttpStatusCode.OK) { } else { //NOK } });

甚至也尝试过如下所示的webget方法.

Even tried a webget method also as below..

RestClient client1 = new RestClient(URL); RestRequest request1 = new RestRequest(string.Format("getdata/{0}", 1), Method.GET); request1.RequestFormat = DataFormat.Json; var x = client1.ExecuteAsync(request1, response => { if (response.StatusCode == HttpStatusCode.OK) { } });

我能够响应.StatusCode为NotFound,即使我检查了提琴手该服务完全没有命中,而且当我验证了服务网址时在作曲家即时得到错误为"不允许HTTP/1.1 405方法"

Im able to the response.StatusCode as NotFound and even when I checked fiddler the service is not hitting at all and when I verified the service url in composer im getting the error as "HTTP/1.1 405 Method Not Allowed"

甚至当我如下尝试使用Webclient

and even when I tried with Webclient as below

string URL = "<servername>:8085/WinPhoneService.svc"; WebClient wclient = new WebClient(); wclient.UseDefaultCredentials = true; wclient.Headers["Content-Type"] = "application/json"; DomainData kpidata = new DomainData(); data.KPIId = 1006; data.ScorecardId = 3; data.EngineeringOrgId = 11; data.DataValuetypeId = 1; data.CumulativeMonth = 463; data.BusinessOrgId = 1; string json = SerializeJson(kpidata); String str = wclient.UploadString(new Uri(URL ),"getkpimeasuredata",json);

即使在这里我正在获得不允许的HTTP/1.1 405方法" ,但在这种情况下,它至少会击中提琴手中所示的服务

Even here im getting "HTTP/1.1 405 Method Not Allowed" but atleast in this case its hitting the service as shown in fiddler

PFB的webconfig相同...

PFB the webconfig for the same...

<?xml version="1.0" encoding="utf-8"?> <configuration> <configSections> <!-- For more information on Entity Framework configuration, visit go.microsoft/fwlink/?LinkID=237468 --> <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" /> </configSections> <appSettings> <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" /> </appSettings> <system.web> <compilation> <assemblies> <add assembly="System.Data.Entity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" /> </assemblies> </compilation> <!--<httpRuntime targetFramework="4.0" />--> </system.web> <system.serviceModel> <bindings> <webHttpBinding> <binding name="webCorpBinding"> <!--<security mode="TransportCredentialOnly"> <transport clientCredentialType="Windows"></transport> </security>--> </binding> </webHttpBinding> </bindings> <behaviors> <endpointBehaviors> <behavior name="RESTCorpBehavior"> <webHttp/> </behavior> </endpointBehaviors> <serviceBehaviors> <behavior> <!-- To avoid disclosing metadata information, set the values below to false before deployment --> <serviceMetadata httpGetEnabled="true" /> <!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information --> <serviceDebug includeExceptionDetailInFaults="false" /> </behavior> </serviceBehaviors> </behaviors> <services> <service name="WinPhoneService.WinPhoneService"> <endpoint name="WinPhoneServiceeCorp" address="" binding="webHttpBinding" bindingConfiguration="webCorpBinding" behaviorConfiguration="RESTCorpBehavior" contract="WinPhoneService.IWinPhoneService" /> </service> </services> <!--<protocolMapping> <add binding="basicHttpsBinding" scheme="https" /> </protocolMapping>--> <!--<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />--> </system.serviceModel> <!--<system.webServer> <modules runAllManagedModulesForAllRequests="true" /> --><!-- To browse web app root directory during debugging, set the value below to true. Set to false before deployment to avoid disclosing web app folder information. --><!-- <directoryBrowse enabled="true" /> </system.webServer>--> <connectionStrings> <add name="ScaasEntities" connectionString="metadata=res://*/Scaas.csdl|res://*/Scaas.ssdl|res://*/Scaas.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=<servername>;initial catalog=<catalog>;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" /> </connectionStrings> <entityFramework> <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" /> </entityFramework> </configuration>

请就此帮我.

我在win8移动应用程序中使用的功能是

the function which I use in win8 mobile app is

public async void LoadKpiData() { string URL = "rr1biscdevsql01:8085/WinPhoneService.svc/getkpimeasuredata; WebClient wclient = new WebClient(); wclient.UseDefaultCredentials = true; wclient.Headers["Content-Type"] = "application/json"; DomainData kpidata = new DomainData(); kpidata.KPIId = 1006; kpidata.ScorecardId = 3; kpidata.EngineeringOrgId = 11; kpidata.DataValuetypeId = 1; kpidata.CumulativeMonth = 463; kpidata.BusinessOrgId = 1; string json = SerializeJson(kpidata); wclient.UploadStringAsync(new Uri(URL), json); wclient.UploadStringCompleted += (s, e) => { }; }

当我检查Restclient呼叫的响应时出现以下错误

and when I check in response for Restclient call got error as below

{"ErrorCode":"1001","ErrorMessage":"System.Data.EntityCommandExecutionException: The data reader is incompatible with the specified 'ScaasModel.GetKPIMeasureData_Result'. A member of the type, 'ParamName', does not have a corresponding column in the data reader with the same name.\u000d\u000a at System.Data.Query.InternalTrees.ColumnMapFactory.GetColumnMapsForType(DbDataReader storeDataReader, EdmType edmType, Dictionary`2 renameList)\u000d\u000a at System.Data.Query.InternalTrees.ColumnMapFactory.CreateColumnMapFromReaderAndType(DbDataReader storeDataReader, EdmType edmType, EntitySet entitySet, Dictionary`2 renameList)\u000d\u000a at System.Data.Query.InternalTrees.ColumnMapFactory.CreateFunctionImportStructuralTypeColumnMap(DbDataReader storeDataReader, FunctionImportMapping mapping, EntitySet entitySet, StructuralType baseStructuralType)\u000d\u000a at System.Data.EntityClient.EntityCommandDefinition.FunctionColumnMapGenerator.System.Data.EntityClient.EntityCommandDefinition.IColumnMapGenerator.CreateColumnMap(DbDataReader reader)\u000d\u000a at System.Data.Objects.ObjectContext.CreateFunctionObjectResult[TElement](EntityCommand entityCommand, EntitySet entitySet, EdmType edmType, MergeOption mergeOption)\u000d\u000a at System.Data.Objects.ObjectContext.ExecuteFunction[TElement](String functionName, MergeOption mergeOption, ObjectParameter[] parameters)\u000d\u000a at System.Data.Objects.ObjectContext.ExecuteFunction[TElement](String functionName, ObjectParameter[] parameters)\u000d\u000a at ScaasWinPhone.Model.ScaasEntities.GetKPIMeasureData(Nullable`1 kPIID, Nullable`1 scorecardID, Nullable`1 cumulativeMonthCount, Nullable`1 engineeringOrgID, Nullable`1 businessOrgID, Nullable`1 dataValueTypeID, Nullable`1 debug, ObjectParameter errorText)\u000d\u000a at ScaasWinphoneRepository.ScaasRepository.GetKpiMeasureData(Nullable`1 kpiId, Nullable`1 scorecardId, Nullable`1 cumulativeMonth, Nullable`1 engineeringOrgId, Nullable`1 businessOrgId, Nullable`1 dataValuetypeId)\u000d\u000a at SCaaSWinPhoneService.SCaaSWinPhoneService.GetKpiMeasureData(KpiDomainData kpidata)"}

拉杰什当我添加 [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)] 和

<serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>

Webconfig中的

出现错误,因为该服务不支持ASP.NET兼容性,因此无法激活.为此应用程序启用了ASP.NET兼容性.在web.config中关闭ASP.NET兼容模式,或将AspNetCompatibilityRequirements属性添加到服务类型,并将RequirementsMode设置设置为允许"或必需".

in webconfig im getting error as The service cannot be activated because it does not support ASP.NET compatibility. ASP.NET compatibility is enabled for this application. Turn off ASP.NET compatibility mode in the web.config or add the AspNetCompatibilityRequirements attribute to the service type with RequirementsMode setting as 'Allowed' or 'Required'.

实际上,当我在localhost中运行服务时,它可以正常工作,但是在托管服务时,它会引发错误..请帮助....

Actually its working fine when I run service in localhost but its throwing the error when I host the service..Please help ....

推荐答案

查看此代码

我的服务器代码如下

[ServiceContract] public interface IWinPhoneService { [OperationContract] [WebInvoke(RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, UriTemplate = "getkpimeasuredata")] List<String> GetKpiMeasureData(DomainData data); } [DataContract] public class DomainData { [DataMember] public int? KPIId { get; set; } [DataMember] public int? ScorecardId { get; set; } [DataMember] public short? CumulativeMonth { get; set; } [DataMember] public int? EngineeringOrgId { get; set; } [DataMember] public int? BusinessOrgId { get; set; } [DataMember] public int? DataValuetypeId { get; set; } }

现在我的界面实现如下

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)] [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)] public class WinPhoneService : IWinPhoneService { public List<string> GetKpiMeasureData(DomainData data) { if (data != null) { return new List<string>() {"Sample 1","Sample 2"}; } return new List<string>() {"No data recieved"}; } }

我的服务器端web.config用于端点元素配置

My server side web.config for endpoint element configuration

<system.serviceModel> <services> <service name="WinPhoneService.WinPhoneService"> <endpoint address="" binding="webHttpBinding" behaviorConfiguration="web" bindingConfiguration="defaultRestJsonp" contract="WinPhoneService.IWinPhoneService"> </endpoint> </service> </services> <behaviors> <endpointBehaviors> <behavior name="web"> <webHttp /> <dataContractSerializer maxItemsInObjectGraph="2147483647"/> </behavior> </endpointBehaviors> </behaviors> <bindings> <webHttpBinding> <binding name="defaultRestJsonp" crossDomainScriptAccessEnabled="true"> <readerQuotas maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxDepth="64" maxNameTableCharCount="2147483647" /> <security mode="None" /> </binding> </webHttpBinding> </bindings> <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" /> </system.serviceModel>

现在已完成所有上述设置,请使用RestSharpClient在下面找到我的客户端代码:

Now with all the above set find my client code below using RestSharpClient:

private static string ToStringUsingDataContractJsonSer<T>(T requestBody) { var serializer = new DataContractJsonSerializer(typeof(T)); var ms = new MemoryStream(); serializer.WriteObject(ms, requestBody); ms.Position = 0; var reader = new StreamReader(ms); return reader.ReadToEnd(); } private string UseRestSharpApproach(string serviceBaseUrl, string resourceUrl, Method method, object requestBody) { var client = new RestClient(); client.BaseUrl = serviceBaseUrl; var request = new RestRequest(method) { DateFormat = DataFormat.Xml.ToString(), Resource = resourceUrl }; request.AddParameter("application/json", requestBody, ParameterType.RequestBody); var response = client.Execute(request); string responseString; if (response.StatusCode == HttpStatusCode.OK) { responseString = HttpUtility.HtmlDecode(response.Content); } else { responseString = response.StatusDescription + " --------------------" + HttpUtility.HtmlDecode(response.Content); } return responseString; } public void CallRESTService() { UseRestSharpApproach(serviceBaseUrl, resourceUrl, Method.POST, ToStringUsingDataContractJsonSer<DomainData>(objDomainData)); }

确保在客户端使用的DomainData对象与服务器使用的命名空间相同,以便进行序列化和反序列化而不会出现问题.

Make sure that the DomainData object when using on the client side is in the same namespace as on server so that the serialization and de-serialization happens without problems.

现在,我从Fiddler发出的原始请求如下:

Now my raw request from Fiddler looks as below:

POST servername/Sample/WinPhoneService.svc/GetKpiMeasureData HTTP/1.1 Accept: application/json, application/xml, text/json, text/x-json, text/javascript, text/xml User-Agent: RestSharp 102.0.0.0 Content-Type: application/json Host: servername Content-Length: 112 Accept-Encoding: gzip, deflate {"BusinessOrgId":1,"CumulativeMonth":463,"DataValuetypeId":1,"EngineeringOrgId":11,"KPIId":1006,"ScorecardId":3}

响应如下:

HTTP/1.1 200 OK Cache-Control: private Content-Length: 23 Content-Type: application/json; charset=utf-8 Server: Microsoft-IIS X-AspNet-Version: 4.0.30319 Set-Cookie: ASP.NET_SessionId=123450p12rvdmnh3qrx2ocip; path=/; HttpOnly Date: Tue, 03 Sep 2013 10:11:51 GMT ["Sample 1","Sample 2"]

更多推荐

Web调用POST“不允许使用方法";在Wcf中

本文发布于:2023-10-11 22:45:03,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1483050.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:不允许   使用方法   POST   Web   quot

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!