Skip to content

Instantly share code, notes, and snippets.

@brendanzagaeski
Last active January 8, 2019 09:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brendanzagaeski/9417060 to your computer and use it in GitHub Desktop.
Save brendanzagaeski/9417060 to your computer and use it in GitHub Desktop.
NullReferenceException in ClientRuntimeChannel constructor on Xamarin.Android 4.12

To solve both of the problems from the stack traces below, follow these steps:

  1. Add the following to an XML file in the project:

    <?xml version="1.0" encoding="utf-8" ?>
    <linker>
    	<assembly fullname="System.ServiceModel">
    		<type fullname="System.ServiceModel.Channels.ChannelFactoryBase`1">
    			<method name="CreateChannel" />
    		</type>
    		<type fullname="System.ServiceModel.FaultException`1">
    			<method name=".ctor" />
    		</type>
    	</assembly>
    </linker>
    
  2. Set the build action of the XML file to "LinkDescription".

  3. Make sure the linker is set to link SDK assemblies only.

Partial stack trace from the exception in the ClientRuntimeChannel constructor:

UNHANDLED EXCEPTION: System.NullReferenceException: Object reference not set to an instance of an object
  at System.ServiceModel.MonoInternal.ClientRuntimeChannel..ctor (System.ServiceModel.Dispatcher.ClientRuntime,System.ServiceModel.Description.ContractDescription,System.TimeSpan,System.TimeSpan,System.ServiceModel.Channels.IChannel,System.ServiceModel.Channels.IChannelFactory,System.ServiceModel.Channels.MessageVersion,System.ServiceModel.EndpointAddress,System.Uri) <0x00508>
  at System.ServiceModel.MonoInternal.ClientRuntimeChannel..ctor (System.ServiceModel.Description.ServiceEndpoint,System.ServiceModel.ChannelFactory,System.ServiceModel.EndpointAddress,System.Uri) <0x000ef>
  at System.ServiceModel.ChannelFactory`1.CreateChannel (System.ServiceModel.EndpointAddress,System.Uri) <0x0013f>
  at System.ServiceModel.ChannelFactory`1.CreateChannel (System.ServiceModel.EndpointAddress) <0x0002b>
  at System.ServiceModel.ChannelFactory`1.CreateChannel () <0x0003b>

The problem here is that the linker in Xamarin.Android 4.12 is removing a CreateChannel() method that it didn't used to remove in 4.10. The ClientRuntimeChannel() constructor accesses this method via reflection.

There is a similar problem with the System.ServiceModel.FaultException constructor:

System.MissingMethodException: No constructor found for System.ServiceModel.FaultException`1[[System.ServiceModel.ExceptionDetail, System.ServiceModel, Version=2.0.5.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]]::.ctor(System.ServiceModel.ExceptionDetail, System.ServiceModel.FaultReason, System.ServiceModel.FaultCode, (unknown))
  at System.Activator.CreateInstance (System.Type type, BindingFlags bindingAttr, System.Reflection.Binder binder, System.Object[] args, System.Globalization.CultureInfo culture, System.Object[] activationAttributes) [0x00000] in :0
  at System.Activator.CreateInstance (System.Type type, System.Object[] args, System.Object[] activationAttributes) [0x00000] in :0
  at System.Activator.CreateInstance (System.Type type, System.Object[] args) [0x00000] in :0
  at System.ServiceModel.MonoInternal.ClientRuntimeChannel.Request (System.ServiceModel.Description.OperationDescription od, System.Object[] parameters) [0x00000] in :0
  at System.ServiceModel.MonoInternal.ClientRuntimeChannel.DoProcess (System.Reflection.MethodBase method, System.String operationName, System.Object[] parameters) [0x00000] in :0
  at System.ServiceModel.MonoInternal.ClientRuntimeChannel.Process (System.Reflection.MethodBase method, System.String operationName, System.Object[] parameters) [0x00000] in :0

@MohsenneChaverdie
Copy link

Thanks man, it helps me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment