测试 WCF 时未列出服务
Service not listed when testing WCF
我正在尝试为我正在制作的网站测试我的 WCF 服务。我正在使用 jQuery 调用服务 ($.getJSON()
),但网站上一直出现连接被拒绝错误。
所以我查看了它在我部署到计算机时制作的网站,甚至没有列出我的方法"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);
});
});
});
首先,如果你改变 webInvoke 属性会更好:
[OperationContract]
[WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json)]
String GetData();
为此:
[OperationContract]
[WebGet(ResponseFormat = WebMessageFormat.Json, UriTemplate="/getData")]
String GetData();
比 运行 你服务并通过这样写 url 在浏览器中打开它:
http://host_name:port_number/service_name.svc/getData
之后你应该得到你的数据(如果一切都好的话)
And when I try to add it, it says that the service is successfully
added, but nothing shows. Earlier today I saw the methods in the list
but had to delete the entire project because I messed up.
我认为这是因为您在 Web 配置文件中设置的 webHttpBinding(它使您的服务成为 restful)。通常测试客户端为 SOAP 服务生成调用方法。
(本来想写评论的,但是写的有点长。。。)
要创建一个简单的休息服务,您必须执行几个步骤:
1)定义服务方法和接口:
namespace CoreAuroraService.Services
{
[DataContract]
public class Patient
{
[DataMember]
public String LastName{get;set;}
[DataMember]
public string FirstName{get;set;}
}
[ServiceContract]
public interface IPatientService
{
[OperationContract]
[WebGet(UriTemplate = "/GetAllPatients", ResponseFormat = WebMessageFormat.Json)]
List<Patient> GetAllPatients();
[OperationContract]
[WebInvoke(UriTemplate = "/Create", Method = "POST", ResponseFormat = WebMessageFormat.Json)]
bool CreatePatient();
[OperationContract]
[WebInvoke(UriTemplate = "/Update", Method = "PUT", ResponseFormat = WebMessageFormat.Json)]
bool UpdatePatient(Guid patientGuid);
[OperationContract]
[WebInvoke(UriTemplate = "/Delete", Method = "DELETE", ResponseFormat = WebMessageFormat.Json)]
bool DeletePatient(Guid patientGuid);
}
public class PatientService : IPatientService
{
public List<Patient> GetAllPatients()
{
var patient = new Patient() { FirstName = "Jeem", LastName = "Street" };
var patients = new List<Patient> { patient };
return patients;
}
public bool CreatePatient()
{
// TODO: Implement the logic of the method here
return true;
}
public bool UpdatePatient(Guid patientGuid)
{
// TODO: Implement the logic of the method here
return true;
}
public bool DeletePatient(Guid patientGuid)
{
// TODO: Implement the logic of the method here
return true;
}
}
}
2) 现在您必须定义服务的行为。为此,您必须更改服务项目中的配置文件。在此文件中,您必须定义服务行为和使您的服务 restful 的其他设置。为此,请在 serviceModel 块中粘贴下一个代码:
<services>
<service name="CoreAuroraService.Services.PatientService">
<endpoint address="" behaviorConfiguration="rest" binding="webHttpBinding" bindingConfiguration="maxStream" contract="CoreAuroraService.Services.IPatientService">
<identity>
<dns value="localhost"/>
</identity>
</endpoint>
<host>
<baseAddresses>
<add baseAddress="https://localhost/Services/PatientService.svc"/>
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior name="rest">
<webHttp helpEnabled="true"/>
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior>
<!-- 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>
</behaviors>
3) 现在让我们编写一个方法,它将从 javascript:
调用我们的服务
<script>
function GetP() {
// Now I need to send cross domain request to the service because my services hosted in another project
// Tested in IE 11
var url = "http://localhost:29358/Services/PatientService.svc/GetAllPatients";
$.ajax({
type: 'GET',
dataType: "text",
url: url,
success: function (responseData, textStatus, jqXHR) {
console.log("in");
var data = JSON.parse(responseData);
console.log(data);
},
error: function (responseData, textStatus, errorThrown) {
alert('POST failed.');
}
});
}
</script>
我正在尝试为我正在制作的网站测试我的 WCF 服务。我正在使用 jQuery 调用服务 ($.getJSON()
),但网站上一直出现连接被拒绝错误。
所以我查看了它在我部署到计算机时制作的网站,甚至没有列出我的方法"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);
});
});
});
首先,如果你改变 webInvoke 属性会更好:
[OperationContract]
[WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json)]
String GetData();
为此:
[OperationContract]
[WebGet(ResponseFormat = WebMessageFormat.Json, UriTemplate="/getData")]
String GetData();
比 运行 你服务并通过这样写 url 在浏览器中打开它:
http://host_name:port_number/service_name.svc/getData
之后你应该得到你的数据(如果一切都好的话)
And when I try to add it, it says that the service is successfully added, but nothing shows. Earlier today I saw the methods in the list but had to delete the entire project because I messed up.
我认为这是因为您在 Web 配置文件中设置的 webHttpBinding(它使您的服务成为 restful)。通常测试客户端为 SOAP 服务生成调用方法。
(本来想写评论的,但是写的有点长。。。) 要创建一个简单的休息服务,您必须执行几个步骤:
1)定义服务方法和接口:
namespace CoreAuroraService.Services
{
[DataContract]
public class Patient
{
[DataMember]
public String LastName{get;set;}
[DataMember]
public string FirstName{get;set;}
}
[ServiceContract]
public interface IPatientService
{
[OperationContract]
[WebGet(UriTemplate = "/GetAllPatients", ResponseFormat = WebMessageFormat.Json)]
List<Patient> GetAllPatients();
[OperationContract]
[WebInvoke(UriTemplate = "/Create", Method = "POST", ResponseFormat = WebMessageFormat.Json)]
bool CreatePatient();
[OperationContract]
[WebInvoke(UriTemplate = "/Update", Method = "PUT", ResponseFormat = WebMessageFormat.Json)]
bool UpdatePatient(Guid patientGuid);
[OperationContract]
[WebInvoke(UriTemplate = "/Delete", Method = "DELETE", ResponseFormat = WebMessageFormat.Json)]
bool DeletePatient(Guid patientGuid);
}
public class PatientService : IPatientService
{
public List<Patient> GetAllPatients()
{
var patient = new Patient() { FirstName = "Jeem", LastName = "Street" };
var patients = new List<Patient> { patient };
return patients;
}
public bool CreatePatient()
{
// TODO: Implement the logic of the method here
return true;
}
public bool UpdatePatient(Guid patientGuid)
{
// TODO: Implement the logic of the method here
return true;
}
public bool DeletePatient(Guid patientGuid)
{
// TODO: Implement the logic of the method here
return true;
}
}
}
2) 现在您必须定义服务的行为。为此,您必须更改服务项目中的配置文件。在此文件中,您必须定义服务行为和使您的服务 restful 的其他设置。为此,请在 serviceModel 块中粘贴下一个代码:
<services>
<service name="CoreAuroraService.Services.PatientService">
<endpoint address="" behaviorConfiguration="rest" binding="webHttpBinding" bindingConfiguration="maxStream" contract="CoreAuroraService.Services.IPatientService">
<identity>
<dns value="localhost"/>
</identity>
</endpoint>
<host>
<baseAddresses>
<add baseAddress="https://localhost/Services/PatientService.svc"/>
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior name="rest">
<webHttp helpEnabled="true"/>
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior>
<!-- 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>
</behaviors>
3) 现在让我们编写一个方法,它将从 javascript:
调用我们的服务<script>
function GetP() {
// Now I need to send cross domain request to the service because my services hosted in another project
// Tested in IE 11
var url = "http://localhost:29358/Services/PatientService.svc/GetAllPatients";
$.ajax({
type: 'GET',
dataType: "text",
url: url,
success: function (responseData, textStatus, jqXHR) {
console.log("in");
var data = JSON.parse(responseData);
console.log(data);
},
error: function (responseData, textStatus, errorThrown) {
alert('POST failed.');
}
});
}
</script>