“Could not find default endpoint element that references contract” … “This might be because no configuration file was found for your application, or because no endpoint element matching this contract could be found in the client element”
Punch nearest person in face!
I like to stick the majority of my code into class libraries, and make it so they have no external dependencies. If the end-application has to implement a “app.config” file, I don’t like this limitation. Here’s a minimal example of a WCF server/client application that removes that requirement (if you want to use callbacks, you need to use the DuplexChannelFactory).
1: // Author: Mike Lovell (mike.lovell@gotinker.com)
2:
3: using System;
4: using System.Collections.Generic;
5: using System.Linq;
6: using System.Text;
7:
8: using System.ServiceModel; // Additional
9:
10:
11: namespace wcfwithoutappconfig
12: {
13: class Program
14: {
15: static void Main(string[] args)
16: {
17: var server = new ServerWrapper();
18: var client = new Client();
19:
20: Console.WriteLine("2 + 2 = {0}", client.CallAdd(2, 2));
21: Console.WriteLine("2 - 2 = {0}", client.CallSubtract(2, 2));
22:
23: server.Close();
24: }
25: }
26:
27:
28: [ServiceContract(Namespace="dotdynamite.local")]
29: interface IServer
30: {
31: [OperationContract]
32: int Add(int x, int y);
33:
34: [OperationContract]
35: int Subtract(int x, int y);
36: }
37:
38:
39: public class ServerWrapper
40: {
41: private ServiceHost serviceHost;
42:
43: public ServerWrapper()
44: {
45: serviceHost = new ServiceHost(typeof(Server));
46:
47: serviceHost.AddServiceEndpoint
48: (
49: typeof(IServer),
50: new NetTcpBinding(),
51: "net.tcp://127.0.0.1:5555/DotDynamite"
52: );
53:
54: serviceHost.Open();
55: }
56:
57:
58: public void Close()
59: {
60: if (serviceHost != null)
61: {
62: serviceHost.Close();
63: serviceHost = null;
64: }
65: }
66: }
67:
68:
69: public class Server : IServer
70: {
71: public int Add(int x, int y)
72: {
73: return x + y;
74: }
75:
76:
77: public int Subtract(int x, int y)
78: {
79: return x - y;
80: }
81: }
82:
83:
84: public class Client
85: {
86: private ChannelFactory<IServer> channel;
87: private IServer server;
88:
89:
90: public Client()
91: {
92: channel = new ChannelFactory<IServer>
93: (
94: new NetTcpBinding(),
95: new EndpointAddress("net.tcp://127.0.0.1:5555/DotDynamite")
96: );
97:
98: server = channel.CreateChannel();
99: }
100:
101:
102: public int CallAdd(int x, int y)
103: {
104: return server.Add(x, y);
105: }
106:
107:
108: public int CallSubtract(int x, int y)
109: {
110: return server.Subtract(x, y);
111: }
112: }
113: }
Download Visual Studio 2010 Project (6.74k)
There are also some other ways around the problem:
- app.config in different files (http://weblogs.asp.net)
- WCF Client without app.config (http://csharpaspnet.blogspot.com)
