Friday, March 27, 2015

When you consume a WCF web service in a C# app you may use a using statement with the client you "new up" in code.

Instantiate like so:

using System.Web.Mvc;
using WebApplication1.LocalServiceReference;
namespace WebApplication1.Controllers
{
   public class HomeController : Controller
   {
      public ActionResult Index()
      {
         using (ConnectivityTesterClient x = new ConnectivityTesterClient())
         {
            x.ClientCredentials.UserName.UserName = "HelloNasty";
            x.ClientCredentials.UserName.Password = "br@ssMonkey";
            x.ClientCredentials.ServiceCertificate.Authentication.CertificateValidationMode =
                  System.ServiceModel.Security.X509CertificateValidationMode.None;
            ViewBag.PassOrFail = x.Test();
         }
         return View();
      }
   }
}

 
 

It didn't even occur to me that the client might implement IDisposable until a coworker said something today. (embarrassing!) When I had to think about it I found some interesting stuff in Googling...

No comments:

Post a Comment