我有一个由Silverlight构建的大项目,在服务器上调用WCF函数。 现在我想使用Ajax从Javascript调用相同的函数。
我可以调用一个不带参数的WCF函数,但在调用带参数的函数时出现异常(错误500)。
这一切对我来说都是一个干净的解决方案,但是我的大项目中有一些错误导致它,我找不到它。
任何帮助将不胜感激。
以下是我的SVC文件:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;
using System.Text;
using System.Xml.Linq;
namespace MyProj.Web
{
[ServiceContract(Namespace = "")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class Service2
{
// To use HTTP GET, add [WebGet] attribute. (Default ResponseFormat is WebMessageFormat.Json)
// To create an operation that returns XML,
// add [WebGet(ResponseFormat=WebMessageFormat.Xml)],
// and include the following line in the operation body:
// WebOperationContext.Current.OutgoingResponse.ContentType = "text/xml";
[OperationContract]
public string Test1() // This function I can call
{
return "1";
}
[OperationContract]
public string Test1WithParam(string catID) //on this function I get the exception
{
return catID;
}
}
}
以下是我的web.config,我相信问题出在某处:
<?xml version="1.0"?>
<!--
For more information on how to configure your ASP.NET application, please visit
http://go.microsoft.com/fwlink/?LinkId=169433
-->
<configuration>
<connectionStrings>
<add name="MyConnectionString" connectionString="Data Source=MyComp\SQLEXPRESS;Initial Catalog=MyDB;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
<system.web>
<roleManager enabled="true"/>
<compilation debug="true" targetFramework="4.0" />
<!--
The <authentication> section enables configuration
of the security authentication mode used by
ASP.NET to identify an incoming user.
-->
<!-- <authentication mode="Windows"/> -->
<authentication mode="Forms">
<!-- The name, protection, and path attributes must match
exactly in each Web.config file. -->
<forms loginUrl="login.aspx" name=".ASPXFORMSAUTH" protection="All" path="/" domain="MyDomain.com" timeout="600"/>
</authentication>
<!-- Validation and decryption keys must exactly match and cannot
be set to "AutoGenerate". The validation and decryption
algorithms must also be the same. -->
<machineKey validationKey="MyValidationkey" decryptionKey="MydecryptionKey" validation="SHA1"/>
<authorization>
<allow roles="administrators"/>
<allow roles="Others"/>
</authorization>
<!--
The <customErrors> section enables configuration
of what to do if/when an unhandled error occurs
during the execution of a request. Specifically,
it enables developers to configure html error pages
to be displayed in place of a error stack trace.
<customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm">
<error statusCode="403" redirect="NoAccess.htm" />
<error statusCode="404" redirect="FileNotFound.htm" />
</customErrors>
-->
</system.web>
<!-- Added for server side authentication data to be available in the WCF service -->
<!-- STX -->
<system.web.extensions>
<scripting>
<webServices>
<authenticationService enabled="true" requireSSL="false"/>
</webServices>
</scripting>
</system.web.extensions>
<!-- ETX -->
<system.serviceModel>
<services>
<service name="MyProj.Web.Service2">
<endpoint address="" behaviorConfiguration="MyProj.Web.Service2AspNetAjaxBehavior"
binding="webHttpBinding" contract="MyProj.Web.Service2" />
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior name="MyProj.Web.Service2AspNetAjaxBehavior">
<enableWebScript />
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"
multipleSiteBindingsEnabled="true" />
</system.serviceModel>
</configuration>
所以我记录了错误消息并得到了: 得到错误:OperationFormatter无法反序列化Message中的任何信息,因为Message为空(IsEmpty = true)。
从这一点开始,根据我所读到的内容,我认为我称之为服务的方式有问题(尽管如前所述,dows是在干净的解决方案上工作)。
所以这是我的JQuery调用:
data = '{"catID":"' + '1' + '"}';
Url = "http://MyProjURL/MyProj.Web/Service2.svc/Test1WithParam";
ContentType = "application/json; charset=utf-8";
DataType = "json";
Type = "POST";
ProcessData = true;
$.ajax({
type: Type, //GET or POST or PUT or DELETE verb
url: Url, // Location of the service
data: Data, //Data sent to server
contentType: ContentType, // content type sent to server
dataType: DataType, //Expected data format from server
processdata: ProcessData, //True or False
crossDomain: true,
success: function (msg) {
//On Successfull service call
testSucceeded(msg);
},
error: testFailed // When Service call fails
});