[英]WCF service client configuration from appsetting.json (.NET Core)
[英]Client configuration to consume WCF JSON web service
我已經將Web服務配置為使用Json,如本博客中所述: http : //www.west-wind.com/weblog/posts/164419.aspx和其他各種博客,但我無法創建客戶端來使用此服務服務。 我嘗試了各種各樣的東西,但總是有無意義的例外。 實現(我應該添加的WCF)客戶端的正確方法是什么?
似乎缺少關於如何為JSON REST服務編寫WCF客戶端的示例。 每個人似乎都使用WCF來實現服務,但幾乎沒有用於編寫客戶端。 所以這是一個相當完整的服務示例(實現GET和POST請求)和客戶端。
服務界面
[ServiceContract]
public interface IService1
{
[OperationContract]
[WebGet(ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Bare,
UriTemplate = "/getcar/{id}")]
Car GetCar(string id);
[OperationContract]
[WebInvoke(RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Bare,
UriTemplate = "/updatecar/{id}")]
Car UpdateCar(string id, Car car);
}
服務數據結構
[DataContract]
public class Car
{
[DataMember]
public int ID { get; set; }
[DataMember]
public string Make { get; set; }
}
服務實施
public class Service1 : IService1
{
public Car GetCar(string id)
{
return new Car { ID = int.Parse(id), Make = "Porsche" };
}
public Car UpdateCar(string f, Car car)
{
return car;
}
}
服務標記
<%@ ServiceHost Language="C#" Service="JSONService.Service1"
CodeBehind="Service1.svc.cs"
Factory="System.ServiceModel.Activation.WebServiceHostFactory" %>
Web.config文件
<?xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
</configuration>
而現在是客戶。 它重用了接口IService1
和類Car
。 此外,還需要以下代碼和配置。
App.config中
<?xml version="1.0"?>
<configuration>
<system.serviceModel>
<behaviors>
<endpointBehaviors>
<behavior name="webby">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
<client>
<endpoint address="http://localhost:57211/Service1.svc" name="Service1" binding="webHttpBinding" contract="JSONService.IService1" behaviorConfiguration="webby"/>
</client>
</system.serviceModel>
</configuration>
Program.cs中
public class Service1Client : ClientBase<IService1>, IService1
{
public Car GetCar(string id)
{
return base.Channel.GetCar(id);
}
public Car UpdateCar(string id, Car car)
{
return base.Channel.UpdateCar(id, car);
}
}
class Program
{
static void Main(string[] args)
{
Service1Client client = new Service1Client();
Car car = client.GetCar("1");
car.Make = "Ferrari";
car = client.UpdateCar("1", car);
}
}
玩得開心。
有什么例外? 它們對你來說可能毫無意義,但是這里的某些人可能會發現它們對診斷你的問題很有幫助。 我使用jQuery向WCF服務發出ajax請求,並且設置通常如下所示:
$(document).ready(function() {
$.ajaxSetup({
type: "POST",
processData: true,
contentType: "application/json",
timeout: 5000,
dataType: "json"
});
var data = { "value": 5 };
AjaxPost("GetData", data, OnEndGetData, OnError);
});
function OnEndGetData(result) {
alert(result.GetDataResult);
}
function OnError(msg) {
alert(msg);
}
function AjaxPost(method, data, callback, error) {
var stringData = JSON.stringify(data);
var url = "Service1.svc/" + method;
$.ajax({
url: url,
data: stringData,
success: function(msg) {
callback(msg);
},
error: error
});
}
JSON.stringify()可以在json.org腳本中找到: http : //www.json.org/js.html ,我的GetData方法的sig如下所示:
[OperationContract]
[WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Wrapped, ResponseFormat = WebMessageFormat.Json)]
string GetData(int value);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.