在 WCF REST 服务中调用 WebGet 时出错
Getting Error when call WebGet in WCF REST Service
我是 WCF 服务的新手。
我哪里做错了。
在动态端口so中创建服务,这样写
客户在 Asp.net
检查一些在线参考资料。但是想不通。
服务器
创建服务:
strAdr = "http://" + strHost + ":" + nPort.ToString() + "/WCFService";
Uri adrbase = new Uri(strAdr);
m_svcHost = new WebServiceHost(typeof(WCFService), adrbase);
ServiceMetadataBehavior mBehave = new ServiceMetadataBehavior();
mBehave.HttpGetEnabled = true;
m_svcHost.Description.Behaviors.Add(mBehave);
m_svcHost.AddServiceEndpoint(typeof(IMetadataExchange),
MetadataExchangeBindings.CreateMexHttpBinding(), "mex");
WebHttpBinding httpb = new WebHttpBinding();
m_svcHost.AddServiceEndpoint(typeof(IWCFService), httpb, strAdr);
m_svcHost.Open();
Debug.WriteLine("\n\nService is Running as >> " + strAdr);
服务Class
[ServiceContract]
public interface IWCFService
{
[WebGet(UriTemplate = "/Test", ResponseFormat = WebMessageFormat.Json,
BodyStyle=WebMessageBodyStyle.Bare)]
[OperationContract]
string Test();
[OperationContract]
[WebInvoke(UriTemplate = "/StoreDetails", RequestFormat = WebMessageFormat.Json, ResponseFormat =
WebMessageFormat.Json, Method = "POST")]
bool StoreDetails(Info_Data configInfo);
}
[AspNetCompatibilityRequirements
(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class WCFService : IWCFService
{
Info_Data config = new config();
public string Test()
{
Console.WriteLine("MSG Come....");
return "Hello";
}
public bool StoreDetails(Info_Data configInfo)
{
config.id = configInfo.id;
config.name = configInfo.name;
}
}
Asp.Net 客户端
[ServiceContract]
public interface IWCFService
{
[OperationContract]
string Test();
[OperationContract]
bool StoreDetails(Info_Data configInfo);
}
WebChannelFactory<IWCFService> chFactMathClient ;
public bool CreateService()
{
strServiceHost = "localhost";
string strEPAdr = "http://" + strServiceHost + ":" + intServicePort.ToString() + "/WCFService";
chFactMathClient = new WebChannelFactory<IWCFService>(new WebHttpBinding(), new Uri(strEPAdr));
System.ServiceModel.Description.WebHttpBehavior behavior = new
System.ServiceModel.Description.WebHttpBehavior();
behavior.DefaultOutgoingRequestFormat = System.ServiceModel.Web.WebMessageFormat.Json;
behavior.DefaultOutgoingResponseFormat = System.ServiceModel.Web.WebMessageFormat.Json;
chFactMathClient.Endpoint.Behaviors.Add(behavior);
mathObj = chFactMathClient.CreateChannel();
return true;
}
当我请求 WebInvoke 时,它工作正常,但为什么它不能与 WebGet 一起工作
不知道接入服务的方式是否正确
chFactMathClient.Test(); //访问数据
服务器和客户端之间的合同不匹配导致了您的问题。所以在 Asp.Net 客户端上添加 WebGet 属性。
[WebGet(UriTemplate = "/Test", ResponseFormat = WebMessageFormat.Json,
BodyStyle=WebMessageBodyStyle.Bare)]
[OperationContract]
string Test();
我是 WCF 服务的新手。 我哪里做错了。
在动态端口so中创建服务,这样写 客户在 Asp.net 检查一些在线参考资料。但是想不通。
服务器
创建服务:
strAdr = "http://" + strHost + ":" + nPort.ToString() + "/WCFService";
Uri adrbase = new Uri(strAdr);
m_svcHost = new WebServiceHost(typeof(WCFService), adrbase);
ServiceMetadataBehavior mBehave = new ServiceMetadataBehavior();
mBehave.HttpGetEnabled = true;
m_svcHost.Description.Behaviors.Add(mBehave);
m_svcHost.AddServiceEndpoint(typeof(IMetadataExchange),
MetadataExchangeBindings.CreateMexHttpBinding(), "mex");
WebHttpBinding httpb = new WebHttpBinding();
m_svcHost.AddServiceEndpoint(typeof(IWCFService), httpb, strAdr);
m_svcHost.Open();
Debug.WriteLine("\n\nService is Running as >> " + strAdr);
服务Class
[ServiceContract]
public interface IWCFService
{
[WebGet(UriTemplate = "/Test", ResponseFormat = WebMessageFormat.Json,
BodyStyle=WebMessageBodyStyle.Bare)]
[OperationContract]
string Test();
[OperationContract]
[WebInvoke(UriTemplate = "/StoreDetails", RequestFormat = WebMessageFormat.Json, ResponseFormat =
WebMessageFormat.Json, Method = "POST")]
bool StoreDetails(Info_Data configInfo);
}
[AspNetCompatibilityRequirements
(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class WCFService : IWCFService
{
Info_Data config = new config();
public string Test()
{
Console.WriteLine("MSG Come....");
return "Hello";
}
public bool StoreDetails(Info_Data configInfo)
{
config.id = configInfo.id;
config.name = configInfo.name;
}
}
Asp.Net 客户端
[ServiceContract]
public interface IWCFService
{
[OperationContract]
string Test();
[OperationContract]
bool StoreDetails(Info_Data configInfo);
}
WebChannelFactory<IWCFService> chFactMathClient ;
public bool CreateService()
{
strServiceHost = "localhost";
string strEPAdr = "http://" + strServiceHost + ":" + intServicePort.ToString() + "/WCFService";
chFactMathClient = new WebChannelFactory<IWCFService>(new WebHttpBinding(), new Uri(strEPAdr));
System.ServiceModel.Description.WebHttpBehavior behavior = new
System.ServiceModel.Description.WebHttpBehavior();
behavior.DefaultOutgoingRequestFormat = System.ServiceModel.Web.WebMessageFormat.Json;
behavior.DefaultOutgoingResponseFormat = System.ServiceModel.Web.WebMessageFormat.Json;
chFactMathClient.Endpoint.Behaviors.Add(behavior);
mathObj = chFactMathClient.CreateChannel();
return true;
}
当我请求 WebInvoke 时,它工作正常,但为什么它不能与 WebGet 一起工作
不知道接入服务的方式是否正确
chFactMathClient.Test(); //访问数据
服务器和客户端之间的合同不匹配导致了您的问题。所以在 Asp.Net 客户端上添加 WebGet 属性。
[WebGet(UriTemplate = "/Test", ResponseFormat = WebMessageFormat.Json,
BodyStyle=WebMessageBodyStyle.Bare)]
[OperationContract]
string Test();