我的 Rest wcf 服务如下...
[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 使用此服务时,如下所示
string URL = "http://<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方法..
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)
{
}
});
我能够将 response.StatusCode 设置为 NotFound ,即使我检查了 fiddler服务根本没有命中,当我在 composer 中验证服务 url 时,我得到错误为“ HTTP/1.1 405 Method Not Allowed ”
甚至当我尝试使用如下 Webclient 时
string URL = "http://<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 Method Not Allowed”但至少在这种情况下它会击中服务,如fiddler所示
PFB 相同的 webconfig...
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/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="data source=<servername>;initial catalog=<catalog>;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
</connectionStrings>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
</entityFramework>
</configuration>
请帮我解决同样的问题..
我在win8移动应用中使用的功能是
public async void LoadKpiData()
{
string URL = "http://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 呼叫的响应时,出现如下错误
{"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)"}
Rajesh 当我添加
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
服务和
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
在 webconfig 中,我收到错误,因为无法激活该服务,因为它不支持 ASP.NET 兼容性。为此应用程序启用了 ASP.NET 兼容性。在 web.config 中关闭 ASP.NET 兼容模式,或将 AspNetCompatibilityRequirements 属性添加到服务类型,并将 RequirementsMode 设置为“允许”或“必需”。
实际上,当我在 localhost 中运行服务时它工作正常,但是当我托管服务时它会抛出错误..请帮助....