WCF Web 服务连接问题
WCF Webservice Connection issue
首先让我说这是我第一次使用 WCF Web 服务,过去 3 天我一直在与错误作斗争。这些问题在 Whosebug 上已经回答了很多次,但是,我已经尝试了大多数解决方案,但还没有成功,所以我需要一些帮助来找出正确的方法。
现在介绍一些背景知识。我正在创建一个 ASP.Net MVC 5 项目,我必须连接到 Epicor(一种 ERP 解决方案)提供的 WCF Web 服务。我的项目、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 绑定时,我使用以下服务参考配置:
在 运行 此配置后,我收到 "Access is denied. Exception Details: System.ServiceModel.Security.SecurityAccessDeniedException: Access is denied."
的错误
对于 nettcp 绑定,当我尝试创建服务引用时,我收到错误消息“无法识别 URI 前缀。元数据包含无法解析的引用:net.tcp:/ /localhost/ERP100700/Ice/BO/Company.svc'。我尝试在 url.
中同时使用 localhost 和 pilotserver
我已经在调试模式 (ISS-Express) 下尝试 运行 应用程序并将其发布到 IIS,但结果相同。我做错了什么,我该如何解决这个问题?
在HomeController
中,好像是这个
CompanyService.ClientCredentials.UserName.UserName = "Administrator";
CompanyService.ClientCredentials.UserName.UserName = "myPassword";
应该是这样的
CompanyService.ClientCredentials.UserName.UserName = "Administrator";
CompanyService.ClientCredentials.UserName.Password = "myPassword
您在代码中传递用户名两次。
我收到所有这些错误的原因是,在 Web.config 中,对于 Epicor 的 Web 服务(在 IIS 内),BasicHTTP 的 https 方案被禁用/注释掉。我必须添加以下内容才能使我的应用程序正常工作。
<remove scheme="https" />
<add scheme="https" binding="basicHttpBinding" bindingConfiguration="BasicHttp"/>
这是 Epicor 中的默认行为。
首先让我说这是我第一次使用 WCF Web 服务,过去 3 天我一直在与错误作斗争。这些问题在 Whosebug 上已经回答了很多次,但是,我已经尝试了大多数解决方案,但还没有成功,所以我需要一些帮助来找出正确的方法。
现在介绍一些背景知识。我正在创建一个 ASP.Net MVC 5 项目,我必须连接到 Epicor(一种 ERP 解决方案)提供的 WCF Web 服务。我的项目、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 绑定时,我使用以下服务参考配置:
在 运行 此配置后,我收到 "Access is denied. Exception Details: System.ServiceModel.Security.SecurityAccessDeniedException: Access is denied."
的错误对于 nettcp 绑定,当我尝试创建服务引用时,我收到错误消息“无法识别 URI 前缀。元数据包含无法解析的引用:net.tcp:/ /localhost/ERP100700/Ice/BO/Company.svc'。我尝试在 url.
中同时使用 localhost 和 pilotserver我已经在调试模式 (ISS-Express) 下尝试 运行 应用程序并将其发布到 IIS,但结果相同。我做错了什么,我该如何解决这个问题?
在HomeController
中,好像是这个
CompanyService.ClientCredentials.UserName.UserName = "Administrator";
CompanyService.ClientCredentials.UserName.UserName = "myPassword";
应该是这样的
CompanyService.ClientCredentials.UserName.UserName = "Administrator";
CompanyService.ClientCredentials.UserName.Password = "myPassword
您在代码中传递用户名两次。
我收到所有这些错误的原因是,在 Web.config 中,对于 Epicor 的 Web 服务(在 IIS 内),BasicHTTP 的 https 方案被禁用/注释掉。我必须添加以下内容才能使我的应用程序正常工作。
<remove scheme="https" />
<add scheme="https" binding="basicHttpBinding" bindingConfiguration="BasicHttp"/>
这是 Epicor 中的默认行为。