Skip to content

Commit

Permalink
Merge branch 'bettererrors' of https://github.com/varkkjuh/WcfClientP…
Browse files Browse the repository at this point in the history
…roxyGenerator into varkkjuh-bettererrors

Conflicts:
	source/WcfClientProxyGenerator.Tests/ProxyTests.cs
  • Loading branch information
jweber committed Nov 12, 2013
2 parents ff68519 + b8837bf commit 739ca7f
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
49 changes: 49 additions & 0 deletions source/WcfClientProxyGenerator.Tests/ProxyTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@ public void Proxy_CanBeGeneratedForOperationWithMixedInputAndOutputParams()
#endregion

#region OnBeforeInvoke and OnAfterInvoke support

[Test]
public void Proxy_OnBeforeInvoke_IsFired()
{
Expand Down Expand Up @@ -633,6 +634,54 @@ public void Proxy_OnAfterInvoke_ReturnValue_ThrowsForVoidMethods()
});
proxy.VoidMethod("test");
}

#endregion

#region Better error messages tests

[ServiceContract]
private interface IPrivateTestService
{
[OperationContract]
void TestMethod();
}

public interface INonServiceInterface
{
[OperationContract]
void TestMethod();
}

[ServiceContract]
public interface INoOperationsInterface
{
void NonOperationTestMethod();
}

[Test]
public void Proxy_GivesProperException_IfInterfaceNotPublic()
{
var mockService = new Mock<IPrivateTestService>();
Assert.Throws<InvalidOperationException>(delegate { WcfClientProxy.Create<IPrivateTestService>(); });
// error message not checked here, but it should be quite readable
}

[Test]
public void Proxy_GivesProperException_IfNotServiceContract()
{
var mockService = new Mock<INonServiceInterface>();
Assert.Throws<InvalidOperationException>(delegate { WcfClientProxy.Create<INonServiceInterface>(); });
// error message not checked here, but it should be quite readable
}

[Test]
public void Proxy_GivesProperException_IfZeroOperationContracts()
{
var mockService = new Mock<INoOperationsInterface>();
Assert.Throws<InvalidOperationException>(delegate { WcfClientProxy.Create<INoOperationsInterface>(); });
// error message not checked here, but it should be quite readable
}

#endregion
}
}
20 changes: 20 additions & 0 deletions source/WcfClientProxyGenerator/DynamicProxyTypeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ internal static class DynamicProxyTypeGenerator<TServiceInterface>
public static Type GenerateType<TActionInvokerProvider>()
where TActionInvokerProvider : IActionInvokerProvider<TServiceInterface>
{
CheckServiceInterfaceValidity(typeof(TServiceInterface));

var moduleBuilder = DynamicProxyAssembly.ModuleBuilder;

var typeBuilder = moduleBuilder.DefineType(
Expand All @@ -65,6 +67,11 @@ public static Type GenerateType<TActionInvokerProvider>()
.SelectMany(t => t.GetMethods(BindingFlags.Public | BindingFlags.Instance))
.Where(t => t.HasAttribute<OperationContractAttribute>());

if (!serviceMethods.Any())
{
throw new InvalidOperationException(String.Format("Service interface {0} has no OperationContact methods. Is this a proper WCF service interface?", typeof(TServiceInterface).Name));
}

foreach (var serviceMethod in serviceMethods)
{
GenerateServiceProxyMethod(serviceMethod, typeBuilder);
Expand All @@ -79,6 +86,19 @@ public static Type GenerateType<TActionInvokerProvider>()
return generatedType;
}

private static void CheckServiceInterfaceValidity(Type type)
{
if (!type.IsPublic && !type.IsNestedPublic)
{
throw new InvalidOperationException(String.Format("Service interface {0} is not declared public. WcfClientProxyGenerator cannot work with non-public service interfaces.", type.Name));
}

if (!type.HasAttribute<ServiceContractAttribute>())
{
throw new InvalidOperationException(String.Format("Service interface {0} is not marked with ServiceContract attribute. Is this a proper WCF service interface?", type.Name));
}
}

private static void SetDebuggerDisplay(TypeBuilder typeBuilder, string display)
{
var attributCtor = typeof(DebuggerDisplayAttribute)
Expand Down

0 comments on commit 739ca7f

Please sign in to comment.