Saturday, July 19, 2014

how to get a WCF web service working in a class library project and not your UI project

At first I tried this:

using Yin.Core.Interfaces;
using Yin.Infrastructure.CalculatorServiceReference;
namespace Yin.Infrastructure.Services
{
   public class MathService : IMathService
   {
      public string Multiply(string firstInput, string secondInput)
      {
         ICalculatorInTheSky calculator = new CalculatorInTheSkyClient();
         Calculation calculation = calculator.Act(firstInput, secondInput);
         return calculation.Result;
      }
   }
}

 
 

And it gave me this error:

Could not find default endpoint element that references contract 'CalculatorServiceReference.ICalculatorInTheSky' in the ServiceModel client configuration section. This might be because no configuration file was found for your application, or because no endpoint element matching this contract could be found in the client element.

 
 

I tried setting up the same service reference in Yin.UserInterface instead of Yin.Infrastructure and it worked like a champ there. So what was different? The difference per this is that while there are some notes about how the service reference should work in the Web.config which will get seen and used at runtime for the Yin.UserInterface implementation, the comparable markup at the app.config in the Yin.UserInterface project will never be looked at at runtime! Boo! I then circled back to this which told how to compensate for what lacked. I refactored my way to success like so:

using System.ServiceModel;
using Yin.Core.Interfaces;
using Yin.Infrastructure.CalculatorServiceReference;
namespace Yin.Infrastructure.Services
{
   public class MathService : IMathService
   {
      public string Multiply(string firstInput, string secondInput)
      {
         BasicHttpBinding basicHttpbinding = new
               BasicHttpBinding(BasicHttpSecurityMode.None);
         basicHttpbinding.Name = "BasicHttpBinding_YourName";
         basicHttpbinding.Security.Transport.ClientCredentialType =
               HttpClientCredentialType.None;
         basicHttpbinding.Security.Message.ClientCredentialType =
               BasicHttpMessageCredentialType.UserName;
         EndpointAddress endpointAddress = new
               EndpointAddress("http://yang/CalculatorInTheSky.svc");
         ICalculatorInTheSky calculator = new CalculatorInTheSkyClient(basicHttpbinding,
               endpointAddress);
         Calculation calculation = calculator.Act(firstInput, secondInput);
         return calculation.Result;
      }
   }
}

 
 

Also, note that whenever I have used Ying and Yang in previous examples it has been inappropriate. It is supposed to be Yin and Yang not Ying and Yang. This is as stupid as using effect instead of affect. Weird Al would hit me with a crowbar I think.

No comments:

Post a Comment