我正在尝试编写一个可由jQuery AJAX函数调用的RESTful WCF服务。我在找出要使用的正确URL时遇到了问题,因此我编写了代码来回调后面的代码,并在代码后面调用服务以确保服务正常运行。所以这就是问题所在。当后面的代码调用一个服务函数时,我得到另一个错误:
类型' System.InvalidOperationException'的例外情况发生在 System.ServiceModel.Web.dll但未在用户代码中处理
其他信息:操作' GetImagesRequest'合同 ' IRetrieveInformation'指定多个请求体参数 没有任何包装元素的序列化。最多一个身体参数 可以序列化没有包装元素。要么删除额外的 body参数或设置BodyStyle属性 WebGetAttribute / WebInvokeAttribute to Wrapped。
我读了很多帖子,据说是BodyStyle = WebMessageBodyStyle.Wrapped
。很容易修复,除了我的方法已经装饰了。我试图在服务中注释掉代码,因为那不是我正在测试的函数,但我总是得到同样的错误。请有人,任何人,帮助!!!
服务接口:
namespace DigitalSignage.Services
{
//[ServiceContract(ConfigurationName = "Data.GetData")]
[ServiceContract]
public interface IRetrieveInformation
{
[OperationContract]
[WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Wrapped, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, UriTemplate = "GetImagesRequest?slideshow={slideshow}")]
string GetImagesRequest(DisplaySlideShow slideshow);
[OperationContract]
[WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Wrapped, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
string GetNewsRequest();
[OperationContract]
[WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Wrapped, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
string GetCalendarRequest(DisplayLocation location);
[OperationContract]
[WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Wrapped, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
string GetLayoutRequest(DisplayLocation location);
}
}
服务实施:
namespace DigitalSignage.Services
{
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class RetrieveInformation : IRetrieveInformation
{
public string GetImagesRequest(DisplaySlideShow slideshow)
{
RetrieveImages ri = new RetrieveImages();
return ri.GetImageLocation(slideshow);
}
public string GetNewsRequest()
{
RetrieveNews rn = new RetrieveNews();
return rn.GetSigmaNews();
}
public string GetCalendarRequest(DisplayLocation location)
{
RetrieveCalendar rc = new RetrieveCalendar();
return rc.GetCalendar(location);
}
public string GetLayoutRequest(DisplayLocation location)
{
RetrieveLayout rl = new RetrieveLayout();
return rl.GetLayout(location);
}
}
}
SERVICE WEB CONFIG:
<?xml version="1.0"?>
<configuration>
<appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
</appSettings>
<system.web>
<compilation debug="true" targetFramework="4.5.2" />
<httpRuntime targetFramework="4.5.2"/>
</system.web>
<system.serviceModel>
<!--<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />-->
<bindings>
<webHttpBinding>
<binding name="Service.WebHttpBinding">
<security mode="TransportCredentialOnly">
<transport clientCredentialType="None"/>
</security>
</binding>
</webHttpBinding>
</bindings>
<services>
<service name="DigitalSignage.Services.RetrieveInformation" behaviorConfiguration="Service.ServiceBehaviors">
<endpoint address=""
contract="DigitalSignage.Services.IRetrieveInformation"
binding="webHttpBinding"
bindingConfiguration="Service.WebHttpBinding"
behaviorConfiguration="Service.EndpointBehaviors"/>
<!-- Enable AJAX Calls -->
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>
</services>
<behaviors>
<endpointBehaviors>
<!-- Enable AJAX Calls -->
<behavior name="ServiceAspNetAjaxBehavior">
<enableWebScript />
</behavior>
<behavior name="Service.EndpointBehaviors">
<webHttp />
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="Service.ServiceBehaviors">
<serviceSecurityAudit auditLogLocation="Application" suppressAuditFailure="true"
serviceAuthorizationAuditLevel="Failure" messageAuthenticationAuditLevel="SuccessOrFailure" />
<serviceMetadata httpGetEnabled="true"/>
</behavior>
<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>
<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>
</configuration>
CALL背后的代码:
[WebMethod]
public static string GetCalendar(string city, string building, string room)
{
JavaScriptSerializer jsonSeralizer = new JavaScriptSerializer();
try
{
string calendarString = ri.GetCalendarRequest(city, building, room).ToString();
return jsonSeralizer.Serialize(calendarString);
}
catch (Exception e)
{
throw e;
}
}
[WebMethod]
public static string GetSigmaNews()
{
JavaScriptSerializer jsonSeralizer = new JavaScriptSerializer();
string news = ri.GetNewsRequest();
return jsonSeralizer.Serialize(news);
}
[WebMethod]
public static string GetImages(string folderName, string city, string building, string room)
{
JavaScriptSerializer jsonSeralizer = new JavaScriptSerializer();
string imagesList = ri.GetImagesRequest(folderName, city, building, room);
return jsonSeralizer.Serialize(imagesList);
}
jQuery AJAX CALL(一个示例):
function getCalendar() {
var dataString = '{"city":' + $('#hdnCity').val() + '", "building":"' + $('#hdnBuilding').val() + '", "room":"' + $('#hdnRoom').val() + '"}';
$.ajax({
type: "Post",
url: "Layout2.aspx/GetCalendar",
data: dataString,
contentType: "application/json; charset=utf-8",
dataType: 'json',
success: function complete(result, jqXHR, status) {
var calendar = JSON.parse(result.d);
$('#calendar').append(calendar);
},
error: function (result, jqXHR, status) {
alert("getCalendar failed because: " + JSON.parse(result.responseText).Message);
}
});
}
FRONT END WEB CONFIG:
<?xml version="1.0" encoding="utf-8"?>
<!--
For more information on how to configure your ASP.NET application, please visit
http://go.microsoft.com/fwlink/?LinkId=169433
-->
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.5.2"/>
<httpRuntime targetFramework="4.5.2" executionTimeout="90" maxRequestLength="11264" />
</system.web>
<system.codedom>
<compilers>
<compiler language="c#;cs;csharp" extension=".cs"
type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
warningLevel="4" compilerOptions="/langversion:6 /nowarn:1659;1699;1701"/>
<compiler language="vb;vbs;visualbasic;vbscript" extension=".vb"
type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
warningLevel="4" compilerOptions="/langversion:14 /nowarn:41008 /define:_MYTYPE=\"Web\" /optionInfer+"/>
</compilers>
</system.codedom>
<system.serviceModel>
<!--<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />-->
<bindings>
<webHttpBinding>
<binding name="Service.WebHttpBinding">
<security mode="TransportCredentialOnly">
<transport clientCredentialType="None"/>
</security>
</binding>
</webHttpBinding>
</bindings>
<client>
<!--<service name="DigitalSignage.Services.IRetrieveInformation.svc" behaviorConfiguration="Service.ServiceBehaviors">-->
<endpoint address="http://localhost:50675/DigitalSignage.Services.IRetrieveInformation"
contract="DigitalSignage.Services.IRetrieveInformation"
binding="webHttpBinding"
bindingConfiguration="Service.WebHttpBinding"
behaviorConfiguration="Service.EndpointBehaviors"/>
<!--</service>-->
</client>
<behaviors>
<endpointBehaviors>
<behavior name="Service.EndpointBehaviors">
<webHttp />
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="Service.ServiceBehaviors">
<serviceSecurityAudit auditLogLocation="Application" suppressAuditFailure="true"
serviceAuthorizationAuditLevel="Failure" messageAuthenticationAuditLevel="SuccessOrFailure" />
<serviceMetadata httpGetEnabled="true"/>
</behavior>
<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>
<protocolMapping>
<add binding="basicHttpsBinding" scheme="https" />
</protocolMapping>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
</system.serviceModel>
</configuration>