WCF Custom tool warning “Cannot import wsdl:portType”

Yet another odd problem to deal with today. “Custom tool warning: cannot import wsdl:portType Could not load file or assembly”Capture

Thanks to Nuget, it appears that it tries to load all assemblies when “Reuse types in all referenced assemblies” is marked.

Capture

The WSDL tool doesn’t use the below setting so it fails.

[code langauge=”xml”]
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="RabbitMQ.Client" publicKeyToken="89e7d7c5feba84ce" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-3.4.3.0" newVersion="3.4.3.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
[/code]

Un-check, deselect, “Reused types in referenced assemblies” and your problems will go away, just don’t check the offending assembly again 🙂 If you have to use that assembly, for your project file to the correct version.

SSMS – Application Intent ReadOnly

If you have an always on MSSQL availability group you can add
applicationintent=readonly;

to your connection string to connect as a read only user on the secondary mirror of your availability group. If you want to connect using Microsoft Sql Server Management Studio (SSMS) using the same intent, open a new window, hit connect, type in your secondary DB instance.

SSMS_CONNECT

Click “Options” and then “Additional Connection Parameters” and viola! You can now run read only queries without taxing your production SQL Sever instance!

SSMS_applicationintent_readonly

Unit Testing Web API Controllers with Headers

I couldn’t find a good example of how to unit test a controller with

[code langauge=”csharp”] var headerValue = Request.Headers.GetValues("HEADER");[/code]

in the controller. Not too common, but happens enough it can be expected. Also the controller inheirts “ApiController” which doesn’t inheirt from “ControllerBase” so the usual controller context call won’t work.

Here is how I coded the unit test

[code language=”csharp”]
var config = new HttpConfiguration();
//add in the actual route or method if your are looking at it in your code
var request = new HttpRequestMessage(HttpMethod.Post, "http://localhost/random");
//DefaultApi maps to your web api route setup
var route = config.Routes.MapHttpRoute("DefaultApi", "api/{controller}/{id}");
//add in the extra data, similar to when you call @Url.Route
var routeData = new HttpRouteData(route, new HttpRouteValueDictionary { { "controller", "products" } });
var controller = new MyProject.Controllers.Api.ExampleController();
controller.ControllerContext = new HttpControllerContext(config, routeData, request);
controller.Request = request;
controller.Request.Headers.Add("X-Your-Header","HeaderValue");
[/code]

In the controller, do a

[code langauge=”csharp”] var headerValue = Request.Headers.GetValues("HEADER");[/code]

do not do

[code langauge=”csharp”]
//DON’T DO THIS
var headerValue = HttpContext.Current.Request.Headers.GetValues("HEADER");[/code]

This will hit the old IIS style objects and your unit test will fail.

If you don’t have Resharper, you might need these using statements.

[code langauge=”csharp”]

using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Web.Http;
using System.Web.Http.Controllers;
using System.Web.Http.Routing;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;

[/code]