【发布时间】:2016-11-12 07:17:09
【问题描述】:
首先让我说这是我第一次使用 WCF Web 服务,过去 3 天我一直在与错误作斗争。这些问题在 Stackoverflow 上已经回答了很多次,但是,我已经尝试了大多数解决方案,但还没有成功,所以我需要一些帮助来找出正确的方法。
现在介绍一些背景。我正在创建一个 ASP.Net MVC 5 项目,我必须连接到 Epicor 提供的 WCF Web 服务(一种 ERP 解决方案)。我的项目、ERP 及其 Web 服务都托管在一个内部 IIS 实例上。这些服务使用 BasicHTTP 和 NetTCP 协议公开。托管 Web 服务和 ERP 的应用程序池使用身份。 其中一项 Web 服务称为 Company.svc,它公开为:
<wsdl:service name="CompanySvcFacade">
<wsdl:port name="BasicHttpBinding_CompanySvcContract" binding="tns:BasicHttpBinding_CompanySvcContract">
<soap:address location="http://pilotserver/ERP100700/Ice/BO/Company.svc"/>
</wsdl:port>
<wsdl:port name="CustomBinding_CompanySvcContract" binding="tns:CustomBinding_CompanySvcContract">
<soap12:address location="net.tcp://pilotserver/ERP100700/Ice/BO/Company.svc"/>
<wsa10:EndpointReference>
<wsa10:Address>net.tcp://pilotserver/ERP100700/Ice/BO/Company.svc</wsa10:Address>
<Identity xmlns="http://schemas.xmlsoap.org/ws/2006/02/addressingidentity">
<Upn>pilotserver\Administrator</Upn>
</Identity>
</wsa10:EndpointReference>
</wsdl:port>
</wsdl:service>
在我的项目中,我的 web.config 有以下内容:
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_CompanySvcContract" />
</basicHttpBinding>
<customBinding>
<binding name="CustomBinding_CompanySvcContract">
<security defaultAlgorithmSuite="Default" authenticationMode="UserNameOverTransport"
requireDerivedKeys="true" includeTimestamp="true" messageSecurityVersion="WSSecurity11WSTrustFebruary2005WSSecureConversationFebruary2005WSSecurityPolicy11BasicSecurityProfile10">
<localClientSettings detectReplays="false" />
<localServiceSettings detectReplays="false" />
</security>
<textMessageEncoding />
<windowsStreamSecurity />
<tcpTransport />
</binding>
</customBinding>
</bindings>
<client>
<endpoint address="http://pilotserver/ERP100700/Ice/BO/Company.svc"
binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_CompanySvcContract"
contract="CompanyService.CompanySvcContract" name="BasicHttpBinding_CompanySvcContract" />
<endpoint address="net.tcp://pilotserver/ERP100700/Ice/BO/Company.svc"
binding="customBinding" bindingConfiguration="CustomBinding_CompanySvcContract"
contract="CompanyService.CompanySvcContract" name="CustomBinding_CompanySvcContract">
<identity>
<userPrincipalName value="pilotserver\Administrator" />
</identity>
</endpoint>
</client>
我正在尝试使用以下代码在客户端中使用 Web 服务:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Description;
using System.ServiceModel.Dispatcher;
using EpicorTestApp.CompanyService;
using NLog;
namespace EpicorTestApp.Controllers
{
public class HomeController : Controller
{
CompanySvcContractClient CompanyService = new CompanySvcContractClient("CustomBinding_CompanySvcContract");
//CompanySvcContractClient CompanyService = new CompanySvcContractClient("BasicHttpBinding_CompanySvcContract");
private Logger logger = LogManager.GetCurrentClassLogger();
public ActionResult Index()
{
ViewBag.Message = "Welcome to ASP.NET MVC!";
bool morePages = false;
CompanyService.ClientCredentials.UserName.UserName = "Administrator";
CompanyService.ClientCredentials.UserName.UserName = "myPassword";
CompanyListTableset companyList = CompanyService.GetList("", 0, 0, out morePages);
CompanyListTable companies = companyList.CompanyList;
foreach (CompanyListRow companyListRow in companies)
{
logger.Info("Company: " + companyListRow.Company);
}
return View();
}
}
}
对于客户端绑定,我尝试了 BasicHttp 和 NetTCP(作为 CustomBinding),都导致了一些错误。创建 BasicHttp 绑定时,我使用以下服务引用配置:
在运行此配置时,我收到“访问被拒绝。异常详细信息:System.ServiceModel.Security.SecurityAccessDeniedException:访问被拒绝”的错误。
对于 nettcp 绑定,当我尝试创建服务引用时,收到错误消息“无法识别 URI 前缀。元数据包含无法解析的引用:net.tcp://localhost/ERP100700 /Ice/BO/Company.svc'。我尝试在 url 中同时使用 localhost 和 Pilotserver。
我尝试在调试模式 (ISS-Express) 下运行应用程序并将其发布到 IIS,但结果相同。我做错了什么,我该如何解决这个问题?
【问题讨论】:
-
生成 net.tcp 客户端代码和类似的合约可能会很棘手。由于它在您的本地 PC 上,请尝试将 Interface Contract 及其依赖的 Data Contracts 复制到客户端应用程序并使用 ChannelFactory 为您制作 net.tcp 代理。至于 BasicHttp ,您需要捕获该请求并查看正在将哪种信息传递给表示您未授权的服务。您可以使用 Fiddler、WCF Tracing 甚至(我强烈推荐)Wireshark 来做到这一点。
-
我提到的一些东西假设你知道基础知识,我不会解释它们,那里有很多谷歌教程和文档。
-
仅供参考,您不能使用
net.tcp协议添加服务引用,您可以通过http来添加。只要服务配置正确地公开了net.tcp端点,您就应该能够根据从http中提取的WSDL 来使用它。net.tcp无需再次添加引用。
标签: asp.net-mvc wcf wcf-binding epicorerp