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

  • SEARCH
  • Home
  • 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 8051289
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T07:16:11+00:00 2026-06-05T07:16:11+00:00

Let’s assume I have a windows forms application that should be remote-controlled or influenced

  • 0

Let’s assume I have a windows forms application that should be remote-controlled or influenced from a remote location with .net.

As far as I’ve googled, hosting a WCF service inside that application would be the way to go. I’ve successfully added a WCF service to the application and can start it with

    ServiceHost host = new ServiceHost(typeof(Service1));   
    host.Open();

What would be a good way to get references to classes from the rest of the running application?
I guess there are only these 2 ways:

  • Use of static properties to call other methods / set properties from within the service method.
  • Assign references to the service class? (How do I assign values to the service hosted within the service host? )

What is considered a good practice or rather which way has worked for you in the past?

Update based on comment
I’m guess I’m looking for something like this:

    ServiceHost host = new ServiceHost(typeof(Service1));   
    host.Open();  
    (Service1)host.MyProperty = "asd";

I can’t seem to find how to cast the ServiceHost (or a property of it) to an instance of Service1. That would probably solve all my problems 😉

  • 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-06-05T07:16:12+00:00Added an answer on June 5, 2026 at 7:16 am

    You can’t really do what you added based on the comment:

    ServiceHost host = new ServiceHost(typeof(Service1));
    host.Open();
    (Service1)host.MyProperty = "asd";
    

    Because at that point no instances of the class Service1 have been created yet. And they’ll only be created when a new request arrives for the service.

    One alternative is to use a custom instance provider (shown in the code below), where you have a reference to the service instance before it’s used by the WCF runtime. You can read more about instance providers at http://blogs.msdn.com/b/carlosfigueira/archive/2011/05/31/wcf-extensibility-iinstanceprovider.aspx.

    public class StackOverflow_10932251
    {
        [ServiceContract]
        public interface ITest
        {
            [OperationContract]
            string Echo(string text);
        }
        public class Service : ITest
        {
            public string MyProperty { get; set; }
            public string Echo(string text)
            {
                Console.WriteLine("Inside Service.Echo, MyProperty = {0}", this.MyProperty);
                return text;
            }
        }
        static Binding GetBinding()
        {
            var result = new WSHttpBinding(SecurityMode.None);
            return result;
        }
        public class MyInstanceProvider : IEndpointBehavior, IInstanceProvider
        {
            string propertyValue;
            public MyInstanceProvider(string propertyValue)
            {
                this.propertyValue = propertyValue;
            }
    
            public void AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
            {
            }
    
            public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
            {
            }
    
            public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
            {
                endpointDispatcher.DispatchRuntime.InstanceProvider = this;
            }
    
            public void Validate(ServiceEndpoint endpoint)
            {
            }
    
            public object GetInstance(InstanceContext instanceContext, Message message)
            {
                return new Service { MyProperty = this.propertyValue };
            }
    
            public object GetInstance(InstanceContext instanceContext)
            {
                return new Service { MyProperty = this.propertyValue };
            }
    
            public void ReleaseInstance(InstanceContext instanceContext, object instance)
            {
            }
        }
        public static void Test()
        {
            string baseAddress = "http://" + Environment.MachineName + ":8000/Service";
            ServiceHost host = new ServiceHost(typeof(Service), new Uri(baseAddress));
            ServiceEndpoint endpoint = host.AddServiceEndpoint(typeof(ITest), GetBinding(), "");
            endpoint.Behaviors.Add(new MyInstanceProvider("asd"));
            host.Open();
            Console.WriteLine("Host opened");
    
            ChannelFactory<ITest> factory = new ChannelFactory<ITest>(GetBinding(), new EndpointAddress(baseAddress));
            ITest proxy = factory.CreateChannel();
            Console.WriteLine(proxy.Echo("Hello"));
    
            ((IClientChannel)proxy).Close();
            factory.Close();
    
            Console.Write("Press ENTER to close the host");
            Console.ReadLine();
            host.Close();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I'm writing a Windows Forms (.NET Framework 3.5) application which shows the
Let assume we have two activities. A - main activity, that is home launcher
let us assume that I have a reusable business layer that further makes use
Let's assume we have the following structure: index.php config.inc.php \ lib \ lib \
Let's assume that I am at manual A. I press enter and I am
Let's say I have a structure named vertex with a method that adds two
Let me state up front that I have an infantile understanding of Monads. I
Let's assume that we are building a high traffic site that will be used
Let's say I have an facebook application running using the JS SDK. First user
Let's say I have multiple requirements for a password. The first is that 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.