我创建了一个宁静的服务,并能够在接收XML请求时使其生成JSON响应,但是我需要它来接受JSON请求。我调整了流程,但遇到了问题,因为我最初拥有的主要服务是调用多个服务,每个服务都有自己的C#接口。它告诉我,我必须明确命名端点,然后所有这些都移到了垃圾堆。我最终放弃了调用多个服务的方法,转而使用具有多个操作合同调用方法的单个服务。
现在,我仅收到一条简单的“找不到端点”消息。我真的非常不了解端点定义的来龙去脉。
这是我的主服务页面(AgtDash.svc)上的代码。我们正在为“代理信息中心”调用统计信息
编辑-更新了代码以反映URI模板引用(未进行任何更改),并更改了web.config中的服务名称引用。现在错误不再是“找不到端点”,而是404参考。
namespace AgentDashboard { public class AgtDash : dashboardInfo { public string GetAgencyInfo(agcInfoInput agcInput) { string agInfo = string.Empty; (RETRIEVES DATA TABLE) agInfo = tableToJson(agcTable); return agInfo; } public string GetAgencyDetails(agcInfoInput agcInput) { string agcyInfo = string.Empty; (RETRIEVES DATA TABLE) agcyInfo = tableToJson(agcTable); return agcyInfo; } public string GetAgencyTotals(agcInfoInput agcInput) { string agcyTot = string.Empty; (RETRIEVES DATA TABLE) agcyTot = tableToJson(agcTable); return agcyTot; } public string GetAgentTotals(agtInfoInput agtInput) { (RETRIEVES DATA TABLE) agtInfo = tableToJson(agTable); return agtInfo; } public string tableToJson(DataTable inpTable) { var jsString = new StringBuilder(); (CONVERTS DATA TABLE TO JSON RESPONSE) return jsString.ToString(); } } }
这是我的界面:
namespace AgentDashboard { [ServiceContract] public interface dashboardInfo { [OperationContract] [WebInvoke(UriTemplate="/GetAgentTotals", Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped)] string GetAgentTotals(agtInfoInput agentInput); [OperationContract] [WebInvoke(UriTemplate="/GetAgencyInfo", Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped)] string GetAgencyInfo(agcInfoInput agencyInput); [OperationContract] [WebInvoke(UriTemplate="/GetAgencyDetails", Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped)] string GetAgencyDetails(agcInfoInput agencyInput); [OperationContract] [WebInvoke(UriTemplate="/GetAgentTotals", Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped)] string GetAgencyTotals(agcInfoInput agencyInput); } [DataContract] public class agtInfoInput { [DataMember] public string id { get; set; } [DataMember] public int year { get; set; } } [DataContract] public class agcInfoInput { [DataMember] public int id { get; set; } [DataMember] public int year { get; set; } } }
我的网络配置:
<system.serviceModel> <client> <endpoint binding="webHttpBinding" contract="AgentDashboard.dashboardInfo" /> </client> <services> <service name="AgentDashboard.AgtDash"> <endpoint address="dashboardInfo" binding="webHttpBinding" behaviorConfiguration="webBehavior" contract="AgentDashboard.dashboardInfo" /> <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/> <host> <baseAddresses> <add baseAddress="AgtDash.svc" /> </baseAddresses> </host> </service> </services> <bindings> <webHttpBinding> <binding> <security mode="None"> <transport clientCredentialType="None" proxyCredentialType="None" realm=""/> </security> </binding> </webHttpBinding> </bindings> <behaviors> <serviceBehaviors> <behavior> <serviceDebug includeExceptionDetailInFaults="true" /> <serviceMetadata httpGetEnabled="true" /> </behavior> </serviceBehaviors> <endpointBehaviors> <behavior name="webBehavior"> <webHttp /> </behavior> </endpointBehaviors> </behaviors> </system.serviceModel>
我确定web.config是一个随机的混乱。什么时候需要定义服务以及端点详细信息?什么时候使用客户端配置?我什么时候都使用?当我最初有多个客户端终结点时,它在本地似乎可以运行,但是当它告诉我必须定义服务时,我就选择了那条路线。
很抱歉,如果那里有答案,但是它们在解决方案中都大不相同,因此我对原因为何没有太多的了解。最终,这将驻留在其自己的Web应用程序/目录中,由IIS提供服务,并且需要接收和发送JSON。可以这么说,JSON响应的生成似乎正在起作用,这是因为正确识别了端点,这使我无法进行全面测试,因为我无法正确识别目标。
如果有关web.config的任何信息突然出现在您面前,请告诉我。预先感谢。
当前错误:
我创建了一个宁静的服务,并能够在接收XML请求时使其生成JSON响应,但是我需要它来接受JSON请求。我调整了流程,但是遇到了问题,因为我...
尝试像这样添加UriTemplate:
[OperationContract]
[WebInvoke(UriTemplate="/GetAgentTotals",
Method = "POST",
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Wrapped)]
string GetAgentTotals(agtInfoInput agentInput)
无需定义额外的服务端点。根据您的配置,操作/方法应采用以下形式。