我正在尝试为我正在制作的网站测试我的WCF服务。我正在使用jQuery来调用服务($.getJSON()
),但我一直在网站上收到Connection Refused Error错误。测试WCF时未列出服务
因此,我检出了部署到计算机时所做的网站,并且我的方法“GetData()”未列出。它只是列出服务本身的名称。我是相当新的一般使用WCF所以怜悯:')在测试客户端的Windows Studio中打开我的服务甚至没有列出:
,当我尝试添加它,它说该服务已成功添加,但没有显示。今天早些时候,我看到列表中的方法,但不得不删除整个项目,因为我搞砸了。
在Web.config看起来是这样的:
<?xml version="1.0"?>
<configuration>
<appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
</appSettings>
<system.web>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5"/>
</system.web>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="ServiceBehavior">
<!-- To avoid disclosing metadata information, set the values below to false before deployment -->
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="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="true"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="EndpBehavior">
<webHttp/>
</behavior>
<behavior name="enableScriptBehavior">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
<services>
<service behaviorConfiguration="ServiceBehavior" name="PUendeligWebService.ExampleService">
<endpoint address="" binding="webHttpBinding"
contract="PUendeligWebService.ExampleServiceInterface" behaviorConfiguration="EndpBehavior"/>
</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"/>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*"/>
</customHeaders>
</httpProtocol>
</system.webServer>
</configuration>
接口:
[ServiceContract]
public interface ExampleServiceInterface
{
[OperationContract]
[WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json)]
String GetData();
[OperationContract]
CompositeType GetDataUsingDataContract(CompositeType composite);
}
[DataContract]
public class CompositeType
{
bool boolValue = true;
string stringValue = "Hello ";
[DataMember]
public bool BoolValue
{
get { return boolValue; }
set { boolValue = value; }
}
[DataMember]
public string StringValue
{
get { return stringValue; }
set { stringValue = value; }
}
}
服务:
public class ExampleService : ExampleServiceInterface
{
public String GetData()
{
Random ran = new Random();
TestClass[] tc = new TestClass[5];
TestClass tc1 = new TestClass();
tc1.TheText = "First Text " + ran.Next();
tc1.TheOtherText = "First Other Text " + ran.Next();
TestClass tc2 = new TestClass();
tc2.TheText = "Second Text " + ran.Next();
tc2.TheOtherText = "Second Other Text " + ran.Next();
TestClass tc3 = new TestClass();
tc3.TheText = "Third Text " + ran.Next();
tc3.TheOtherText = "Third Other Text " + ran.Next();
TestClass tc4 = new TestClass();
tc4.TheText = "Fourth Text " + ran.Next();
tc4.TheOtherText = "Fourth Other Text " + ran.Next();
TestClass tc5 = new TestClass();
tc5.TheText = "Fifth Text " + ran.Next();
tc5.TheOtherText = "Fifth Other Text " + ran.Next();
tc[0] = tc1;
tc[1] = tc2;
tc[2] = tc3;
tc[3] = tc4;
tc[4] = tc5;
return JsonConvert.SerializeObject(tc);
}
public CompositeType GetDataUsingDataContract(CompositeType composite)
{
if (composite == null)
{
throw new ArgumentNullException("composite");
}
if (composite.BoolValue)
{
composite.StringValue += "Suffix";
}
return composite;
}
}
最后,只是良好的措施,这里是我的jQuery用途:
$(function() {
$("input:button").click(function() {
$.getJSON("http://localhost:52535/ExampleService.svc/GetData?callback=?", function(data) {
alert(data);
});
});
});
不特定的端口,没有。我正在使用Netbeans将网站部署到自己的端口上。 – OmniOwl
尝试在** Release **而不是Debug中构建解决方案,然后再次运行。我在尝试使用WCF测试客户端时遇到了类似的问题,并且在我更改构建模式时提供了帮助,因为它显然需要发布构建中的.dll文件。 – urbz
@urbz它没有工作。它具有完全相同的行为:/ – OmniOwl