【问题标题】:WCF Webservice Connection issueWCF Web服务连接问题
【发布时间】: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


【解决方案1】:

HomeController,好像是这个

CompanyService.ClientCredentials.UserName.UserName = "Administrator";
CompanyService.ClientCredentials.UserName.UserName = "myPassword";

应该是这样的

CompanyService.ClientCredentials.UserName.UserName = "Administrator";
CompanyService.ClientCredentials.UserName.Password = "myPassword

您在代码中传递了用户名两次。

【讨论】:

  • 我不敢相信这个错字。感谢您发现这一点。但是,错误仍然相同。不过再次感谢您。
  • 正确发送密码后,使用HTTP(BasicHttpBinding_CompanySvcContract)客户端时是否还会出现拒绝访问错误?
  • 是的,同样的错误。可能此时要问的问题是如何正确设置 webconfig 和/或客户端代码中的凭据?我已经尝试了“CompanyService.ClientCredentials.UserName.UserName/Password”和“CompanyService.ChannelFactory.Credentials.UserName.UserName/Passowrd”组合。此外,在 Windows 事件查看器中,我注意到我收到“服务授权失败”错误,客户端身份为空“ClientIdentity:”。
  • 另一件要考虑的事情:Thread.CurrentPrincipal。值得一搜。创建一个GenericPrincipal,填充它,将其添加到该属性。 (??)
【解决方案2】:

我收到所有这些错误的原因是在 Epicor 的 Web 服务(在 IIS 内部)的 Web.config 中,BasicHTTP 的 https 方案被禁用/注释掉了。我必须添加以下内容才能使我的应用程序正常工作。

  <remove scheme="https" />
  <add scheme="https" binding="basicHttpBinding" bindingConfiguration="BasicHttp"/> 

这是 Epicor 中的默认行为。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-11
    • 2011-09-30
    • 2019-02-08
    相关资源
    最近更新 更多