Search code examples
asp.net-mvcwcfwcf-bindingepicorerp

WCF Webservice Connection issue


Let me start by saying that this is my first time working with WCF web services and I've been battling error for the last 3 days. These issues have been answered many times at Stackoverflow, however, I've tried most solutions and haven't been successful yet, so I need some help in figuring out the right way.

Now some background. I'm creating an ASP.Net MVC 5 project, I've to connect to WCF web services provided by Epicor (an ERP solution). My project, the ERP and its web services are all hosted on an internal IIS instance. The services are exposed using both BasicHTTP and NetTCP protocols. The application pool on which the web service and ERP are hosted uses identity. One of the web service is called Company.svc and it is exposed as:

<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>

In my project, my web.config has the following:

<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>

And I'm trying to consume the web service in the client using the following code:

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();
        }
    }
}

For the client binding, I've tried both BasicHttp and NetTCP (as CustomBinding), both resulting in some errors. When I create a BasicHttp binding, I use the following service reference configuration:

BasicHttpBinding

and upon running this configuration, I receive an error for "Access is denied. Exception Details: System.ServiceModel.Security.SecurityAccessDeniedException: Access is denied."

BasicHttp Error

And for nettcp binding, when I try to create a service reference, I receive an error with the message "The URI prefix is not recognized. Metadata contains a reference that cannot be resolved: net.tcp://localhost/ERP100700/Ice/BO/Company.svc'. I've tried using both localhost and pilotserver in the url.

NetTcp Error

I've tried running the application both in debug mode (ISS-Express) and publishing it to IIS, but same result. What am I doing wrong and how can I resolve this issue?


Solution

  • The reason why I was getting all these errors was that in Web.config for Epicor's web services (inside IIS), https scheme for BasicHTTP was disabled/ comment out. I had to add the following to make my application work.

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

    This is the default behavior in Epicor.