Skip to content

Commit

Permalink
Expose IErrorHandler and add test case.
Browse files Browse the repository at this point in the history
  • Loading branch information
imcarolwang committed Nov 18, 2024
1 parent 43b91da commit 6bf8c8c
Show file tree
Hide file tree
Showing 8 changed files with 190 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -766,6 +766,11 @@ public static string DuplexCallbackDebugBehavior_Address
{
get { return GetEndpointAddress("DuplexCallbackDebugBehavior.svc/tcp", protocol: "net.tcp"); }
}

public static string DuplexCallbackErrorHandler_Address
{
get { return GetEndpointAddress("DuplexCallbackErrorHandler.svc/tcp", protocol: "net.tcp"); }
}
#endregion net.tcp Addresses

#region net.pipe Addresses
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Description;
using System.ServiceModel.Dispatcher;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
Expand Down Expand Up @@ -287,6 +288,11 @@ public static void Run(Func<Task> asyncMethod)
}
}

[DataContract]
public class CustomMessage
{
}

[DataContract(Namespace = "http://www.contoso.com/wcfnamespace")]
public class CompositeType
{
Expand Down Expand Up @@ -705,6 +711,53 @@ public void ReplyThrow(string input)
}
}

[CallbackBehavior(UseSynchronizationContext = false)]
public class WcfDuplexService_CallbackErrorHandler_Callback : IWcfDuplexService_CallbackErrorHandler_Callback, IEndpointBehavior, IErrorHandler
{
public void ReplyThrow(string input)
{
throw new FaultException<ArgumentException>(new ArgumentException());
}

void IEndpointBehavior.AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
{
return;
}

void IEndpointBehavior.ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
{
clientRuntime.CallbackDispatchRuntime.ChannelDispatcher.ErrorHandlers.Add(this);
}

void IEndpointBehavior.ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
{
return;
}

void IEndpointBehavior.Validate(ServiceEndpoint endpoint)
{
return;
}

#region IErrorHandler Members
public bool HandleError(Exception error)
{
return true;
}

public void ProvideFault(Exception error, MessageVersion version, ref Message msg)
{
if (error is FaultException<ArgumentException>)
{
//overriding the given fault to CustomMessage fault
var faultObj = new FaultException<CustomMessage>(new CustomMessage(), "custom fault reason", new FaultCode("custom fault code"));
MessageFault fault = faultObj.CreateMessageFault();
msg = Message.CreateMessage(version, fault, "http://tempuri.org/IWcfDuplexService_CallbackErrorHandler/ReplyThrowCustomMessageFault");
}
}
#endregion
}

public class DuplexTaskReturnServiceCallback : IWcfDuplexTaskReturnCallback
{
private bool _wrapExceptionInTask;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,19 @@ public interface IWcfDuplexService_CallbackDebugBehavior_Callback
void ReplyThrow(string input);
}

[ServiceContract(CallbackContract = typeof(IWcfDuplexService_CallbackErrorHandler_Callback))]
public interface IWcfDuplexService_CallbackErrorHandler
{
[OperationContract]
bool Hello(string greeting);
}

public interface IWcfDuplexService_CallbackErrorHandler_Callback
{
[OperationContract]
void ReplyThrow(string input);
}

[ServiceContract(CallbackContract = typeof(IWcfDuplexTaskReturnCallback))]
public interface IWcfDuplexTaskReturnService
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.


using System.ServiceModel;
using Infrastructure.Common;
using Xunit;

public partial class CallbackErrorHandlerTests : ConditionalWcfTest
{
[WcfFact]
public static void DuplexCallbackErrorHandlerTest()
{
bool result;
InstanceContext context;
string testString="";
NetTcpBinding binding;
EndpointAddress endpointAddress;
DuplexChannelFactory<IWcfDuplexService_CallbackErrorHandler> factory;
IWcfDuplexService_CallbackErrorHandler serviceProxy;
WcfDuplexService_CallbackErrorHandler_Callback callbackService;

// *** SETUP *** \\
binding = new NetTcpBinding();
binding.Security.Mode = SecurityMode.None;
callbackService = new WcfDuplexService_CallbackErrorHandler_Callback();
context = new InstanceContext(callbackService);
endpointAddress = new EndpointAddress(Endpoints.DuplexCallbackErrorHandler_Address);
factory = new DuplexChannelFactory<IWcfDuplexService_CallbackErrorHandler>(context, binding, endpointAddress);
serviceProxy = factory.CreateChannel();

try
{
// *** EXECUTE *** \\
result = serviceProxy.Hello(testString);

// *** CLEANUP *** \\
((ICommunicationObject)serviceProxy).Close();
factory.Close();
}
finally
{
// *** ENSURE CLEANUP *** \\
ScenarioTestHelpers.CloseCommunicationObjects((ICommunicationObject)serviceProxy, factory);
}

// *** ADDITIONAL VALIDATION *** \\
Assert.True(result);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -372,3 +372,8 @@ public PingEncodedRequest(string pinginfo)
this.Pinginfo = pinginfo;
}
}

[DataContract]
public class CustomMessage
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,18 @@ public interface IWcfDuplexService_CallbackDebugBehavior_Callback
[OperationContract]
void ReplyThrow(string input);
}

[ServiceContract(CallbackContract = typeof(IWcfDuplexService_CallbackErrorHandler_Callback))]
public interface IWcfDuplexService_CallbackErrorHandler
{
[OperationContract]
bool Hello(string greeting);
}

public interface IWcfDuplexService_CallbackErrorHandler_Callback
{
[OperationContract]
[FaultContract(typeof(CustomMessage))]
void ReplyThrow(string input);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -149,4 +149,37 @@ public IWcfDuplexService_CallbackDebugBehavior_Callback Callback
}
}
}

[ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Multiple)]
public class WcfDuplexService_CallbackErrorHandler : IWcfDuplexService_CallbackErrorHandler
{
public bool Hello(string greeting)
{
bool result = false;
try
{
Callback.ReplyThrow(greeting);
}
catch (FaultException<CustomMessage> fex)
{
if(fex.Message.Equals("custom fault reason") && fex.Code.Name.Equals("custom fault code"))
{
result = true;
}
}
catch (Exception)
{
}

return result;
}

public IWcfDuplexService_CallbackErrorHandler_Callback Callback
{
get
{
return OperationContext.Current.GetCallbackChannel<IWcfDuplexService_CallbackErrorHandler_Callback>();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -152,4 +152,20 @@ public DuplexCallbackDebugBahaviorServiceHost(params Uri[] baseAddresses)
{
}
}

[TestServiceDefinition(Schema = ServiceSchema.NETTCP, BasePath = "DuplexCallbackErrorHandler.svc")]
public class DuplexCallbackErrorHandlerServiceHost : TestServiceHostBase<IWcfDuplexService_CallbackErrorHandler>
{
protected override string Address { get { return "tcp"; } }

protected override Binding GetBinding()
{
return new NetTcpBinding(SecurityMode.None) { PortSharingEnabled = false };
}

public DuplexCallbackErrorHandlerServiceHost(params Uri[] baseAddresses)
: base(typeof(WcfDuplexService_CallbackErrorHandler), baseAddresses)
{
}
}
}

0 comments on commit 6bf8c8c

Please sign in to comment.