HRESULT: 0x80131515 when running installutil to install a .NET Service

So you write your nice and fancy service and are all excited to distribute it to your client. You don’t feel like creating an installer for it, that seems like overkill. You just send a zip file with a batch file that calls installutil to register the service. Easy right?

Well, your client calls and complains that get the following error when running installutil aka your batch file:

Exception occurred while initializing the installation:

System.IO.FileLoadException: Could not load file or assembly ‘file:///D:servicesmyservicehost.exe’ or one of its dependencies. Operation is not support. (Exception from HRESULT: 0x80131515).

I am sure you will start scratching your head and immediately blame them missing a dll or maybe the client ran it wrong. Maybe they have it in the wrong directory? Seems feasible, right? Wrong.

It turns out that Windows was protecting them. Windows doesn’t like files from other computers very much, especially not executables. They get a special stay away from me flag.

So, how do I fix this? Easy. Just open the properties dialog on the exe/service you extracted and hit the UNBLOCK button. Now installutil will work great!

Happy Coding!

CommunicationException with Silverlight and a cross domain call

You are calling your services from Silverlight but the services are accessed via a different URI than the Silverlight application is being hosted. Errors galore, right? 

Silverlight honors the protection that helps mitigate a sites scripts from being called if someone simply copied your app to their server. 

The error you get is this: 

An error occurred while trying to make a request to URI ‘http://localhost.:2935/Services/UserService.svc’. This could be due to attempting to access a service in a cross-domain way without a proper cross-domain policy in place, or a policy that is unsuitable for SOAP services. You may need to contact the owner of the service to publish a cross-domain policy file and to ensure it allows SOAP-related HTTP headers to be sent. This error may also be caused by using internal types in the web service proxy without using the InternalsVisibleToAttribute attribute. Please see the inner exception for more details. 

This error is very easily fixed. 

Create a file in you web root. Must be the root called clientaccesspolicy.xml . It must be named exactly and must live in the root. 

File: clientaccesspolicy.xml

Now, copy this into the file: 

<?xml version="1.0" encoding="utf-8"?>
<access-policy>
  <cross-domain-access>
    <policy>
      <allow-from http-request-headers="SOAPAction">
        <domain uri="*"/>
      </allow-from>
      <grant-to>
        <resource path="/" include-subpaths="true"/>
      </grant-to>
    </policy>
  </cross-domain-access>
</access-policy>

Save it, rebuild and run. Voila, you should now be up and running. If you need more info, check this helpful link. 

Happy Coding!

FedEx Integration Address Service: Unable to generate a temporary class…ParsedElement[]

Are you using the new WCF service reference against the FedEx Address service WSDL? I’ll bet your scratching your head on why you get this craptastic error.

 InnerException: {"Unable to generate a temporary class (result=1).rnerror CS0030: Cannot convert type 'YOURNAMESPACE.FedExAddressService.ParsedElement[]' to 'YOURNAMESPACE.FedExAddressService.ParsedElement'rnerror CS0029: Cannot implicitly convert type 'YOURNAMESPACE.FedExAddressService.ParsedElement' to 'YOURNAMESPACE.FedExAddressService.ParsedElement[]'rn"}
    Message: "There was an error in serializing body of message addressValidationRequest1: 'Unable to generate a temporary class (result=1).rnerror CS0030: Cannot convert type 'YOURNAMESPACE.FedExAddressService.ParsedElement[]' to 'YOURNAMESPACE.FedExAddressService.ParsedElement'rnerror CS0029: Cannot implicitly convert type 'YOURNAMESPACE.FedExAddressService.ParsedElement' to 'YOURNAMESPACE.FedExAddressService.ParsedElement[]'rn'.  Please see InnerException for more details."
    Source: "mscorlib"

The answer is annoying. You would think running on the .NET 4.0 Framework they would have figured out their bugs with consuming WSDL’s.

So, open your references.cs file for the service. (It’s hidden, you either go to disk or hit the little icon that says show all files, hit the arrow on the service reference, then you see the magic file)

Hit Ctrl+H and type in [][] for the find and replace with [].

Basically, it made this nice two dimensional array that the server is less than pleased to receive.

If you refresh from the WSDL, guess what? You get to do this all over again, so quit hitting the refresh button.

Happy coding!

InstallUtil and BadImageFormat with a normal C# service

So, you get the old BadImageFormat error when installing your service. If you aren’t writing C++ extensions, then this is probably because you forgot to use the right version of InstallUtil for your target platform.

i.e. x86 build using x64 InstallUtil or vice versa.

Easiest fix? Just build for Any CPU. Then you really don’t have to think about it too hard.

Happy coding!

How to write your own Windows Communication Foundation proxy

Note: This article assumes you already know how to write the WCF service. I am just giving you an example of how to talk to it without using the goofy auto-generated class.

I see a lot of developers who write WCF services like to use the auto-generated proxy classes for communicating with there service. The problem is: What if you want to add custom code to the proxies? Your changes are going to get wiped when you refresh the proxy class.

If you don’t plan on ever doing anything more complicated with your service, or you simply want to take the easy route, by all means use the auto-generated proxy from Visual Studio. This will work for most people. If you want more control, and to be able to update the proxy on your own terms, then you can use the following code and tips to write your own hand rolled proxy code.

It is still very simple to write and use, plus it looks so much cleaner.

You will need to first create your contracts, which you need no matter how you wish to write your service. The contracts for Window Communication Foundation services are your key to allowing the client application to speak to your service. It is best practice to write explicit contracts so you can separate your business entities from your data transfer objects (DTO’s), but you already knew that.

Let’s pretend that our Service Contract, the contract that represents the methods available for invocation by a client, is defined as IMyService for the sake of our example.

Since you may have multiple services, always plan for the future, you will want to write a base proxy that you can inherit from in your proxy class.

Let’s define the base proxy to be nice and simple. In the future, or a future article, you can expand the proxy to allow for adding to the message headers or utilize more advanced WCF features. Base Proxy Definition:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography.X509Certificates;
using System.Configuration;
using System.ServiceModel;
using System.Windows.Forms;

namespace PwC.Audition.Server.Proxies.ClientServices
{

    /// <summary>
    /// Use for handling some core functionality like cert handling and security
    /// </summary>
    public abstract class BaseProxyClient<TChannel> : System.ServiceModel.ClientBase<TChannel> where TChannel : class
    {
        string userId;

        // I like to tell my service what version I am, I usually throw this into the headers. But that’s for a later article.
        protected static string ClientVersion
        {
            get
            {
                return ConfigurationSettings.AppSettings["ClientVersion"];
            }
        }

        /// <summary>
        /// Creates the secured context scope. Put IMMEDIATELY into a USING statement. Must be disposed.
        /// </summary>
        /// <param name="channel">The channel.</param>
        /// <returns>Active operation context scope prefilled with security descriptors</returns>
        protected OperationContextScope CreateSecuredContextScope()
        {
            OperationContextScope scope = new OperationContextScope((IClientChannel)Channel);

            //Out of scope of this post, but I put all of my header entries here by writing to the operation scope.
            // you then add the userID and other items to the header
            return scope;

        }

        /// <summary>
        /// Inits the security information.
        /// </summary>
        private void InitSecurityInformation()
        {
            //  userId = GetCurrentUser(); simple example of storing the user id from whatever location you wish. This is then used to put into header
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="BaseProxyClient"/> class.
        /// </summary>
        public BaseProxyClient()
        {
            InitSecurityInformation();
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="BaseProxyClient<TChannel>"/> class.
        /// </summary>
        /// <param name="endpointConfigurationName">Name of the endpoint configuration.</param>
        public BaseProxyClient(string endpointConfigurationName) :
            base(endpointConfigurationName)
        {
            InitSecurityInformation();
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="BaseProxyClient<TChannel>"/> class.
        /// </summary>
        /// <param name="endpointConfigurationName">Name of the endpoint configuration.</param>
        /// <param name="remoteAddress">The remote address.</param>
        public BaseProxyClient(string endpointConfigurationName, string remoteAddress) :
            base(endpointConfigurationName, remoteAddress)
        {
            InitSecurityInformation();
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="BaseProxyClient<TChannel>"/> class.
        /// </summary>
        /// <param name="endpointConfigurationName">Name of the endpoint configuration.</param>
        /// <param name="remoteAddress">The remote address.</param>
        public BaseProxyClient(string endpointConfigurationName, System.ServiceModel.EndpointAddress remoteAddress) :
            base(endpointConfigurationName, remoteAddress)
        {
            InitSecurityInformation();
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="BaseProxyClient<TChannel>"/> class.
        /// </summary>
        /// <param name="binding">The binding.</param>
        /// <param name="remoteAddress">The remote address.</param>
        public BaseProxyClient(System.ServiceModel.Channels.Binding binding, System.ServiceModel.EndpointAddress remoteAddress) :
            base(binding, remoteAddress)
        {
            InitSecurityInformation();
        }

    }
}

Using the base proxy, we can now create our class to handle the interface of our service. Note, I included a simple template in the base proxy for adding header information. You can easily omit the InitSecurityInformation call as well as the CreateSecuredContextScope if you don’t ever plan to work with message headers. If there is enough demand, I can cover the usage of the message headers in WCF and how they can be very useful for passing information to the server more easily. The service below shows an example of a contract that has two methods. IsAlive and TotalUptime. You would implement the actual methods and interface for your particular needs. If you notice how easy it is to implement, a simple call to base.Channel.MethodName lets you call your service.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Seekford.App.Server.Contracts.MyService //<---YOUR SERVICE CONTRACT HERE
using System.Collections.ObjectModel;

namespace Seekford.App.Server.Proxies
{
    /// <summary>
    /// Proxy to utilize the My Service.
    /// </summary>
    public class MyServiceClient : BaseProxyClient<IMyService> , IMyService //<-- Implement the interface for the contract. If you don't want to implement every method, you can then remove this and not full implement all of them. Useful for exposing only a subset of your service.
    {

         #region constructors
        /// <summary>
        /// Initializes a new instance of the <see cref=" MyServiceClient "/> class.
        /// </summary>
        /// <param name="endpointConfigurationName">Name of the endpoint configuration.</param>
        public MyServiceClient(string endpointConfigurationName) :
            base(endpointConfigurationName)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref=" MyServiceClient "/> class.
        /// </summary>
        /// <param name="endpointConfigurationName">Name of the endpoint configuration.</param>
        /// <param name="remoteAddress">The remote address.</param>
        public MyServiceClient (string endpointConfigurationName, string remoteAddress) :
            base(endpointConfigurationName, remoteAddress)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref=" MyServiceClient "/> class.
        /// </summary>
        /// <param name="endpointConfigurationName">Name of the endpoint configuration.</param>
        /// <param name="remoteAddress">The remote address.</param>
        public MyServiceClient (string endpointConfigurationName, System.ServiceModel.EndpointAddress remoteAddress) :
            base(endpointConfigurationName, remoteAddress)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref=" MyServiceClient "/> class.
        /// </summary>
        /// <param name="binding">The binding.</param>
        /// <param name="remoteAddress">The remote address.</param>
        public MyServiceClient (System.ServiceModel.Channels.Binding binding, System.ServiceModel.EndpointAddress remoteAddress) :
            base(binding, remoteAddress)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="MyServiceClient"/> class.
        /// </summary>
        public MyServiceClient ()
            : base("TCP_IMyService")
        {

        }

        #endregion

        /// <summary>
        /// Determines whether this instance is alive.
        /// </summary>
        /// <returns>
        /// 	<c>true</c> if this instance is alive; otherwise, <c>false</c>.
        /// </returns>
        public bool IsAlive()
        {
            using (var scope = CreateSecuredContextScope())
            {
                return base.Channel.IsAlive();
            }
        }

        /// <summary>
        /// Totals the uptime.
        /// </summary>
        /// <returns></returns>
        public TimeSpan TotalUptime()
        {
            using (var scope = CreateSecuredContextScope())
            {
                return base.Channel.TotalUptime();
            }
        }

    }
}

Of course, you need to make sure you either setup your app.config with the connection information or you supply the server information to the constructor at run time. If adding to the configuration file, the following is a simple example of what you would need to do. I bolded the items that are pertinent. You will need to match your configuration to your needs. If you are using http for example.

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.serviceModel>
    <bindings>
      <netTcpBinding>
        <binding name="TCP_Binding" closeTimeout="00:30:00" openTimeout="00:01:00" receiveTimeout="00:30:00" sendTimeout="00:30:00" maxBufferSize="500000000" maxReceivedMessageSize="500000000">
          <readerQuotas maxDepth="32" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2000000" maxNameTableCharCount="16384"/>
        </binding>
      </netTcpBinding>
    </bindings>
    <client>
     <endpoint address="net.tcp://MyServer:9090/seekford/1.0/MyService"   binding="netTcpBinding"
               bindingConfiguration="TCP_Binding"
               contract="Seekford.App.Server.Contracts.MyService"
               name="TCP_MyService">
        <identity>
          <dns value="localhost" />
        </identity>
      </endpoint>
   </client>
  </system.serviceModel>
</configuration>

Now that you see how simple it is to write your own clean proxy class, you will wonder why you were always using the auto generated proxies.

It is so simple and clean.

Happy coding!