473,473 Members | 2,309 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Connecting to a WCF Service hosted in a Windows Service

I have written a simple WCF service hosted in a Windows console application
and a simple WCF client console application that connects successfully to
that service and retrieves data. I then ported the console application to
WinForms and also got that to work properly.

My next move was to host the WCF service in a Windows service application. I
believe I have that working correctly, but I can't for the life of me get a
client to connect to the service. I did not use svcutil to generate proxy
code for the client, but instead relied on using a ChannelFactory to
instantiate my client connection to the service.

Here's the client code:

namespace MyClient
{
public partial class MainForm : Form
{
private IMyService service;

public MainForm()
{
InitializeComponent();

try
{
ChannelFactory<IMyServicefactory = new
ChannelFactory<IMyService>("MyService");
service = factory.CreateChannel();
string myData = service.GetMyData();
MessageBox.Show(myData);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message + ex.StackTrace);
}
}
}
}

Here's the client app.config:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<client>
<endpoint address="net.tcp://localhost:12345/MyService/service"
binding="netTcpBinding"
contract="Contracts.IMyService"
name="MyService">
</endpoint>
</client>
</system.serviceModel>
</configuration>

Here's the server app.config:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<services>
<service name="MyService">
<host>
<baseAddresses>
<add
baseAddress="net.tcp://localhost:12345/MyService/service"/>
</baseAddresses>
</host>
<endpoint address=""
binding="netTcpBinding"
contract="Contracts.IMyService">
</endpoint>
<endpoint address="mex"
binding="mexTcpBinding"
contract="IMetadataExchange">
</endpoint>
</service>
</services>
</system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="metadataSupport">
<serviceMetadata>
</serviceMetadata>
</behavior>
</serviceBehaviors>
</behaviors>
</configuration>

Here's the contract code:

namespace Contracts
{
[ServiceContract]
public interface IMyService
{
[OperationContract]
string GetMyData();
}
}

I get the following exception when invoking the service.GetMyData() method:

Could not connect to net.tcp://localhost:12345/MyService/service. The
connection attempt lasted for a time span of 00:00:00.9843813. TCP error code
10061: No connection could be made because the target machine actively
refused it.
Server stack trace:
at System.ServiceModel.Channels.SocketConnectionIniti ator.Connect(Uri
uri, TimeSpan timeout)
at System.ServiceModel.Channels.BufferedConnectionIni tiator.Connect(Uri
uri, TimeSpan timeout)
at
System.ServiceModel.Channels.ConnectionPoolHelper. EstablishConnection(TimeSpan timeout)
at
System.ServiceModel.Channels.ClientFramingDuplexSe ssionChannel.OnOpen(TimeSpan timeout)
at System.ServiceModel.Channels.CommunicationObject.O pen(TimeSpan timeout)
at System.ServiceModel.Channels.ServiceChannel.OnOpen (TimeSpan timeout)
at System.ServiceModel.Channels.CommunicationObject.O pen(TimeSpan timeout)
at
System.ServiceModel.Channels.ServiceChannel.CallOp enOnce.System.ServiceModel.Channels.ServiceChannel .ICallOnce.Call(ServiceChannel
channel, TimeSpan timeout)
at
System.ServiceModel.Channels.ServiceChannel.CallOn ceManager.CallOnce(TimeSpan
timeout, CallOnceManager cascade)
at System.ServiceModel.Channels.ServiceChannel.Ensure Opened(TimeSpan
timeout)
at System.ServiceModel.Channels.ServiceChannel.Call(S tring action,
Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs,
TimeSpan timeout)
at System.ServiceModel.Channels.ServiceChannel.Call(S tring action,
Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs)
at
System.ServiceModel.Channels.ServiceChannelProxy.I nvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation)
at System.ServiceModel.Channels.ServiceChannelProxy.I nvoke(IMessage
message)

Exception rethrown at [0]:
at System.Runtime.Remoting.Proxies.RealProxy.HandleRe turnMessage(IMessage
reqMsg, IMessage retMsg)
at System.Runtime.Remoting.Proxies.RealProxy.PrivateI nvoke(MessageData&
msgData, Int32 type)
at Contracts.IMyService.GetMyData()
at WinClient.MainForm.GetMyData() in C:\Documents and Settings\adrian\My
Documents\Visual Studio 2005\Projects\WCFTest\WinClient\MainForm.cs:line 54

This is my first attempt at using WCF. Any help would be greatly appreciated.
Mar 19 '07 #1
0 8250

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Matt | last post by:
I know it is possible to establish a connection to an SQL Server over the Internet via Enterprise Manager. My question is, how secure is this connection? Is there a best practices document for this...
2
by: Fadi | last post by:
Backround: I am trying to figure out how to do the equivalant of a classic COM Local Server Singleton in .NET/C#. I created a coupld of simple Class Libs that exposes public interfaces and hosted...
8
by: Julia | last post by:
Hi, My client uses a remote object when I host the remote object inside a windows application all seem to work fine but when I host the same object inside a windows service I get the following...
3
by: GTDriver | last post by:
I'm trying to connect my application with a web service located on my own web server(localhost). I guess when the solution/proect is built it makes a file called 'Web...
3
by: Big Charles | last post by:
Hi, We have developed an ASP.NET web application and are planning to host it in an external Server, which provides us a good bandwidht. We need to put he web application outside because the...
1
by: Karch | last post by:
I am writing a WCF Service using MSMQ hosted in a Windows Service on Windows XP. All the required components are installed (.NET3, MSMQ, etc). For some reason my service tries to start and then...
10
by: mairhtin o'feannag | last post by:
Hello, I'm having problems connecting to my new v9 db box. The pertinent information is below: DB2_db2inst1 60000/tcp DB2_db2inst1_1 60001/tcp DB2_db2inst1_2 60002/tcp DB2_db2inst1_END...
5
by: Oriane | last post by:
Hi, I have a .Net Visual Studio solution with one asp.net Web application, which uses a Asp.Net Web service. Both are compiled in debug mode, and both are using the Studio development web server...
0
by: khurramlone | last post by:
I am trying to generate a client proxy from a WCF service library. I am using VS2005, .NET 3.0 on a Windows XP Pro workstation. The WCF service is hosted in a windows service. When I run the windows...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.