WCF Service References not generating web.config client endpoints and bindings

by Tom Hundley 7. December 2011 14:57

Problem

I hate to admit it, but I spent a few hours pulling my hair out on this one.  I created a new WCF Service and added a few services.  I then added a service reference to some small consuming applications and everything worked fine.  I then added a service reference to a large website which has about ~50 referenced assemblies.  This time when I did it, my web.config serviceModel section wasn’t updated by Visual Studio 2010.  Normally, Studio will generate bindings and client end points for you.  What the hell!  I was baffled. 

I soon discovered that the website wouldn’t compile with this WCF service reference either, which was odd because I obviously hadn’t written any consuming code for it yet.  The compiler error was this:

 

Parser Error Message: Reference.svcmap: Failed to generate code for the service reference XXXXOrderService'.
Cannot import wsdl:portType
Detail: An exception was thrown while running a WSDL import extension:

System.ServiceModel.Description.DataContractSerializerMessageContractImporter
Error: Could not load file or assembly …
 

 

Solution

As it turns out, the problem was happening because Reuse types in all referenced assemblies was enabled in the service reference settings, which is is enabled by default.  Well, that wasn’t WHY the error was occurring and I haven’t taken the time to become well versed enough in this matter (behind on a deadline), but this can apparently cause issues if both the consuming application and the WCF service reference the same assemblies.  I didn’t actually spend the time figuring out which assembly was causing the problem, but that wouldn’t matter to you anyway.  I simply unchecked Reuse types for now and everything started working; I deleted the service reference, re-added it, and the web.config was properly updated with the client endpoints and service bindings.  Badda Boom.  Service Model happiness.

If you don’t know how to get to this, you can either click the advanced button when adding the service reference or configure after it’s been added by clicking Configure Service Reference on the svcmap. 

Untitled-1

I hope this helps someone.

Happy coding,

Tom Hundley

Tags:

Visual Studio | WCF

Team Explorer 2008 and TFS 2010

by Tom Hundley 21. July 2011 08:35

Forward Compatibility Update

Imagine a hell where you’re forced to use VS 2008 in 2011. Such is my life right now. It’s not all bad, however, because we’re implementing TFS 2010. I discovered some nuances getting Team Explorer 2008 to work with TFS 2010.

 

The standard steps are these:

 

  1. 1. Install Visual Studio 2008 Service Pack 1 (VS 2008 SP1)
  2. 2. Install the Visual Studio Team System 2008 Service Pack 1 Forward Compatibility Update for Team Foundation Server 2010 (Installer)

 

Easy enough. Here are the nuances.

 

Unable to connect to this Team Foundation Server

To connect to TFS, you have to use the full URL as the server name. If you don’t and you just use the server name, you’ll get this:

 

TFS31002: Unable to connect to this Team Foundation Server.

Possible reasons for failure include:
- The Team Foundation Server name , port number or protocol is incorrect.
- The Team Foundation Server is offline
- Password is expired or incorrect

 

image

 

Solution

So, instead of using “SERVERNAME” as the server name, include the full URL which by default would be:

http://SERVERNAME:8080/tfs and you should be able to connect with no problem.

 

Unable to switch servers at this time. The Team Explorer is busy.

In addition to this annoying error (you’ll get it for each Team Project you choose), work items are not available in Team Explorer. WTF! This happens when you include a "/” at the end of your URL.

 

image

 

Solution

Remove the forward slash from your server’s URL and badda bing, it work’s like a champ. A big thinks to “Uglybugger” for posting this fix here.

 

Works: http://SERVERNAME:8080/tfs

Doesn’t work: http://SERVERNAME:8080/tfs/

 

Hopefully this helps someone, although I can’t imagine there are many people left on this planet still using VS 2008.

 

Happy Coding,

Tom Hundley

Tags:

Team Foundation Server | Visual Studio

Implementing CLR User Defined Function (UDF) in SQL 2008 R2

by Tom Hundley 21. April 2011 07:05

Implementing a CLR User Defined Function (UDF) in SQL 2008 R2 is very easy.  Note that this code is not limited to 2008 R2 or UDFs, and implementing CLR stored procedures is just as simple.

 

The steps are as follows:

 

1.  Create you class with all desired methods / functionality. Note that your classes and methods should be static!

 

2.  Add appropriate attributes to the methods you want to utilize as CLR objects.  For a UDF, you add the [SqlFunction] attribute.  For a CLR stored procedure, you would use [SqlProcedure].  Note that these attributes are in the Microsoft.SqlServer.Server namespace.

 

3.  It’s considered best practice to install register your assembly with SQL Server using Partial Trust.  To that end, you must Allow Partially Trusted Callers in your assembly’s AssemblyInfo class.  Do this my simply adding [assembly: AllowPartiallyTrustedCallers].

   1: using System;
   2: using System.Collections.Generic;
   3: using System.Linq;
   4: using System.Text;
   5: using Microsoft.SqlServer.Server;
   6:  
   7: namespace Demo.ClrUdf
   8: {
   9:     public static class MyClrUdfClass
  10:     {
  11:         [SqlFunction]
  12:         public static string HelloWorld()
  13:         {
  14:             return "giddyup";
  15:         }
  16:     }
  17: }
   1: using System.Reflection;
   2: using System.Runtime.CompilerServices;
   3: using System.Runtime.InteropServices;
   4:  
   5: // General Information about an assembly is controlled through the following 
   6: // set of attributes. Change these attribute values to modify the information
   7: // associated with an assembly.
   8: [assembly: AssemblyTitle("Demo.ClrUdf")]
   9: [assembly: AssemblyDescription("")]
  10: [assembly: AssemblyConfiguration("")]
  11: [assembly: AssemblyCompany("Microsoft")]
  12: [assembly: AssemblyProduct("Demo.ClrUdf")]
  13: [assembly: AssemblyCopyright("Copyright © Microsoft 2011")]
  14: [assembly: AssemblyTrademark("")]
  15: [assembly: AssemblyCulture("")]
  16:  
  17: // Used becuase we're not giving the assemby full trust in SQL
  18: [assembly: AllowPartiallyTrustedCallers]
  19:  
  20:  
  21: // Setting ComVisible to false makes the types in this assembly not visible 
  22: // to COM components.  If you need to access a type in this assembly from 
  23: // COM, set the ComVisible attribute to true on that type.
  24: [assembly: ComVisible(false)]
  25:  
  26: // The following GUID is for the ID of the typelib if this project is exposed to COM
  27: [assembly: Guid("63b93754-c707-4610-b1f4-c6e5949c0a99")]
  28:  
  29: // Version information for an assembly consists of the following four values:
  30: //
  31: //      Major Version
  32: //      Minor Version 
  33: //      Build Number
  34: //      Revision
  35: //
  36: // You can specify all the values or you can default the Build and Revision Numbers 
  37: // by using the '*' as shown below:
  38: // [assembly: AssemblyVersion("1.0.*")]
  39: [assembly: AssemblyVersion("1.0.0.0")]
  40: [assembly: AssemblyFileVersion("1.0.0.0")]

 

AllowPartialTrustCallers

 

4.  Sign your assembly.  The easiest way to do this is to use a new string key name file by right clicking on your project, click on the Signing Tab, and click Sign the assembly.  This will create a PFX certificate in your project folder that is used to create the signature.

 

Sign Assembly with SKN

 

5.  You’re done with the assembly.  Now on to SQL Server.  First, you need to make sure that the CLR is enabled on the server.

   1: -- First, run this to enable the CLR.  This only has to be done once.
   2: sp_configure 'show advanced options', 1;
   3: GO
   4: RECONFIGURE;
   5: GO
   6: sp_configure 'clr enabled', 1;
   7: GO
   8: RECONFIGURE;
   9: GO
  10:  
  11:  
  12:  
  13:  

 

6.  Next, you must install / register your assembly with SQL Server.  This is simple.

   1: -- Run this, changing the names and paths appropriately (path is relative to the server)
   2: -- Note the Safe permission set- this is best practice
   3: -- Thus, you must set "[assembly: AllowPartiallyTrustedCallers]" in your AssemblyInfo.cs (and all referenced assemblies
   4: -- Note that this will automatically register dependent / referenced assemblies
   5:  
   6: USE [MyDatabase]
   7:  
   8: CREATE ASSEMBLY DemoUserDefinedFunctions
   9: FROM 'D:\Demo\Demo.ClrUdf.dll'
  10: WITH PERMISSION_SET = SAFE
  11:  
  12:  
  13: --DROP ASSEMBLY DemoUserDefinedFunctions
  14:  
  15: --SELECT * FROM sys.assemblies

 

7.  Finally, Create the CLR UDF or CLR Stored Procedure.  Here’s an example of creating a UDF with the assembly we just registered.  I used a simple Demo class for this blog, but I left one my of implementation of a real UDF in this sample so you can see how to pass parameters, etc.  Note, the only thing that might trick you up a big is the return type of the methods.  For strings, it has to support Unicode, so use nvarchar().

 

   1: -- Enable the CLR if it isn't already done
   2: -- Register the assembly
   3: -- Change the names and parameters of the UDF appropriatley
   4: -- Input and output must match the method
   5: -- Note the SQL to CLR mappings are obviously important- string only maps to unicode so we'll have to convert in our packages
   6:  
   7: USE [MyDatabase]
   8:  
   9: SET ANSI_NULLS ON
  10: GO
  11: SET QUOTED_IDENTIFIER ON
  12: GO
  13:  
  14: --CREATE FUNCTION EncryptUsernamePassword(@clearText nvarchar(255)) RETURNS nvarchar(255) 
  15: --AS EXTERNAL NAME XXXUserDefinedFunctions.[XXX.Database.Clr.UserDefinedFunctions.XXXCryptography].EncryptUsernamePassword
  16: --GO
  17:  
  18: CREATE FUNCTION HelloWorld() RETURNS nvarchar(255) 
  19: AS EXTERNAL NAME DemoUserDefinedFunctions.[Demo.ClrUdf.MyClrUdfClass].HelloWorld
  20: GO
  21:  
  22: -- DROP FUNCTION EncryptUsernamePassword
  23:  

 

8.  You’re done.  Just call your UDf or sproc from your T-SQL.  EzPz.

 

Happy Coding,

 

Tom Hundley

Tags:

C# | SQL Server