HTTPS WCF web service web config

Here’s the web config for a WCF web service. I struggled getting it to work under HTTPS. But when all done and working it’s not terrible – I’ll try to highlight the relevant names because that was my primary struggle point. I won’t go into explaining everything, there are tons of posts out there can (and do) explain much better than I can or want to here.

WebConfig

I pieced together several different examples in an attempt to get this working and at first my highlighted names didn’t match – and no luck working. So here are the highlighted sections of the serviceModel element with the names matching up.

See ServiceBehaviorA, wsHttpBinding, and TransportSecurity. These are referenced in the endpoint and must also be referenced in the behaviors and bindings elements.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
  <system.serviceModel>
 
    <services>
      <service name="S2C.API.Service" 
 
               <!-- SEE BELOW in serviceBehaviors -->
               behaviorConfiguration="ServiceBehaviorA">
 
        <endpoint address="" 
 
                  <!-- SEE BELOW in bindings -->
                  binding="wsHttpBinding"
 
                  <!-- SEE BELOW the binding name -->
                  bindingConfiguration="TransportSecurity"
 
                  contract="S2C.API.IService" />
 
        <endpoint address="mex" 
                  binding="mexHttpBinding" 
                  contract="IMetadataExchange" />
      </service>
    </services>
 
    <behaviors>
      <serviceBehaviors>
        <behavior name="ServiceBehaviorA">
 
          <!-- To avoid disclosing metadata information, 
               set the value below to false and remove 
               the metadata endpoint above before deployment -->
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
 
          <!-- To receive exception details in faults for debugging purposes, 
               set the value below to true. Set to false before deployment 
               to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="true"/>
 
        </behavior>
      </serviceBehaviors>
    </behaviors>
 
    <bindings>
      <wsHttpBinding>
        <binding name="TransportSecurity">
 
          <security mode="Transport">
            <transport clientCredentialType="None"/>
          </security>
 
        </binding>
      </wsHttpBinding>
    </bindings>
 
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
 
  </system.serviceModel>

Dump .NET References

An application that I work on has many (in excess of 50) referenced custom .NET assemblies. Most of the custom assemblies follow the same version number scheme – but not all of them. And at either the beginning or end of a release cycle, the version numbers change and it can be difficult to track and ensure you have the proper assemblies referenced.

Red Gate’s .NET Reflector is a nice tool to examine an assembly but I need a way to plow through my bin folder and log all of my custom assemblies referenced assemblies, including the version numbers.

The code below does that. (Full disclosure, this code is a mixture of a couple of examples I found on the internet made to work for my situation.)

All of my custom assemblies have a distinct naming convention so I call the CollectAndLogReferences method with this regular expression “^(AAA|AAB|AAC)” to filter only my custom dlls and executables. With the filtering like this, I don’t log all of the system dlls and others that I don’t care about.

I paste the output (which is a lot of text) into my text editor. From there, I can easily search, sort, etc. as needed.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
...
CollectAndLogReferences("^(AAA|AAB|AAC)");
 
 
public void CollectAndLogReferences(string sReferenceExpression)
{
	var sRefExp = "^(.)";
	if (sReferenceExpression.Length > 0) 
		sRefExp = sReferenceExpression;
 
	int indent = 0;
	var loggedAssembly = new List<string>();
	var referencedAssembly = new List<string>();
 
	var p = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
	var files = Directory.GetFiles(p, "*.*").Where(f => f.EndsWith(".exe") || f.EndsWith(".dll"));
	foreach (string file in files)
	{
		try
		{
			var assembly = Assembly.LoadFile(file);
			if (!Regex.IsMatch(assembly.FullName, sRefExp))
				continue;
 
			if (!referencedAssembly.Contains(assembly.FullName))
				referencedAssembly.Add(assembly.FullName);
		}
		catch (Exception e) { }
	}
 
	while (referencedAssembly.Count > 0)
	{
		LogAssemblies(indent, sRefExp, loggedAssembly, referencedAssembly);
	}
}
 
 
public void LogAssemblies(int indent, string sRefExp, List<string> logged, List<string> referenced)
{
	Display(indent, "");
	var referencedAssembly2 = new List<string>();
	foreach (var refd in referenced)
	{
		if (logged.Contains(refd)) continue;
		try
		{
			Assembly r = Assembly.Load(refd);
			Display(indent, "Assembly: {0}", r);
			Display(indent, "Referenced assemblies:");
			foreach (AssemblyName an2 in r.GetReferencedAssemblies())
			{
				if (Regex.IsMatch(an2.Name, sRefExp))
				{
					Display(indent + 1, "Name={0}, Version={1}, Culture={2}, PublicKeyToken={3}", 
						an2.Name, an2.Version, an2.CultureInfo.Name, (BitConverter.ToString(an2.GetPublicKeyToken())));
					if (!referencedAssembly2.Contains(an2.FullName))
						referencedAssembly2.Add(an2.FullName);
				}
			}
			logged.Add(r.FullName);
			Display(indent, "");
		}
		catch (Exception e)
		{
			Display(indent, "EXCEPTION DUMP BEGIN * * * *");
			Display(indent, refd);
			Display(indent, e.ToString());
			Display(indent, "EXCEPTION DUMP END * * * * *");
		}
	}
 
	referenced.Clear();
	referenced.AddRange(referencedAssembly2);
}
 
 
public void Display(int indent, string format, params object[] param)
{
	var str = string.Format(format, param);
	//Output.Text += new string(' ', indent * 2);
	//Output.Text += str + Environment.NewLine;
	Console.Write(new string(' ', indent * 2));
	Console.WriteLine(str);
}