Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3460844
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T10:16:46+00:00 2026-05-18T10:16:46+00:00

I have a WCF service with a custom IServiceBehavior attached used to return a

  • 0

I have a WCF service with a custom IServiceBehavior attached used to return a specific fault on the client side. When I enable this code with TCP Message Security I receive a service timeout.

Below you can see the full client and server code to reproduce the error.

Server code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.ServiceModel.Dispatcher;
using System.ServiceModel.Channels;

namespace TestWCFServer
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("SERVER");

            NetTcpBinding binding = new NetTcpBinding();
            binding.Security.Mode = SecurityMode.Message; //If you remove this line the code works!!!!

            Uri address = new Uri("net.tcp://localhost:8184/");

            // Create the ServiceHost.
            using (ServiceHost host = new ServiceHost(typeof(HelloWorldService)))
            {
                host.AddServiceEndpoint(typeof(IHelloWorldService), binding, address);

                host.Description.Behaviors.Add(new MyErrorhandlerBehavior());

                host.Open();

                Console.WriteLine("The service is ready at {0}", address);
                Console.WriteLine("Press  to stop the service.");
                Console.ReadLine();

                // Close the ServiceHost.
                host.Close();
            }
        }
    }



    [ServiceContract]
    public interface IHelloWorldService
    {
        [OperationContract]
        string SayHello(string name);
    }

    public class HelloWorldService : IHelloWorldService
    {
        public string SayHello(string name)
        {
            if (name == null)
                throw new ArgumentNullException("name");

            return string.Format("Hello, {0}", name);
        }
    }

    class MyErrorhandlerBehavior : IServiceBehavior, IErrorHandler
    {
        #region IServiceBahvior
        public void AddBindingParameters(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase, System.Collections.ObjectModel.Collection endpoints, BindingParameterCollection bindingParameters)
        {
        }

        public void ApplyDispatchBehavior(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
        {
            foreach (ChannelDispatcher chanDisp in serviceHostBase.ChannelDispatchers)
            {
                chanDisp.ErrorHandlers.Add(this);
            }
        }

        public void Validate(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
        {
        }
        #endregion

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

        public void ProvideFault(Exception error,MessageVersion ver, ref Message msg)
        {
            FaultException fe = new FaultException(error.Message);
            MessageFault fault = fe.CreateMessageFault();
            msg = Message.CreateMessage(ver, fault, "net.tcp://localhost:8184/fault");
        }
        #endregion
    }
}

Client code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Description;
using System.ServiceModel.Dispatcher;

namespace TestWCFClient
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("CLIENT");

            try
            {
                NetTcpBinding binding = new NetTcpBinding();
                binding.Security.Mode = SecurityMode.Message; //If you remove this line the code works!!!!

                Uri address = new Uri("net.tcp://localhost:8184/");
                EndpointAddress endpoint = new EndpointAddress(address);

                HelloWorldServiceClient client = new HelloWorldServiceClient(binding, endpoint);

                Console.WriteLine("Calling client with a valid parameter...");
                Console.WriteLine(client.SayHello("Davide"));
                Console.WriteLine("OK");

                Console.WriteLine("Calling client with an invalid parameter...");
                Console.WriteLine(client.SayHello(null)); //This call causes the timeout when Security is set to Message
                Console.WriteLine("OK");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }


            Console.WriteLine("Press enter to exit");
            Console.ReadLine();
        }
    }


    [ServiceContract]
    public interface IHelloWorldService
    {
        [OperationContract]
        string SayHello(string name);
    }

    class HelloWorldServiceClient : System.ServiceModel.ClientBase, IHelloWorldService
    {
        public HelloWorldServiceClient(System.ServiceModel.Channels.Binding binding, System.ServiceModel.EndpointAddress address) :
            base(binding, address)
        { }

        public string SayHello(string name)
        {
            return base.Channel.SayHello(name);
        }
    }
}

If I remove the binding.Security.Mode = SecurityMode.Message; line on the client and on the server the exception is correctly translated and the client can see it without problems.

On the WCF log I see these messages:
No signature message parts were specified for messages with the ‘net.tcp://localhost:8184/fault’ action.
The security protocol cannot secure the outgoing message.

Any idea on how to solve this problem? Seems that I must sign/encrypt the error message but I don’t known how…
If I use Transport security the code works as expected.

thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T10:16:46+00:00Added an answer on May 18, 2026 at 10:16 am

    It might be a bit late to respond but I had the same issue and I figured it out:

    You need to define FaultContract on your interface methods along with OperationContract. Have a look here.

    Also your fault contract object needs to have the same namespace as the Action namespace.

    Sooooooooooo painful but finally figured out and works now.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code in WCF service to throw a custom fault based
I have a WCF service with three methods. Two of the methods return custom
I have a WCF service and a client, that uses a custom UserNamePasswordValidator for
I have a WCF service that is using a custom ServiceAuthorizationManager . The custom
I have a secure WCF service with custom authentication. When I am stress testing
I have a WCF service that I secure with a custom UserNamePasswordValidator and Message
I have a WCF service that needs to expose a custom collection to it's
We currently have a situation where we have a WCF service which generates custom
I have a WCF Data Service for which I have a custom URL Rewriter
I have a WCF service that returns JSON to the clients. This is the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.