WCF Exception: InvalidOperationException was unhandled
Since I'm contemplating to use WCF I thought it would be best to just follow a simple tutorial to get my feet wet.
3 hours later, I've only got one exception to show for. It won't go away.
I ruled out the app.config not being loaded. If I change: wsHttpBinding in the config to JHGHJGH it gives an error when running. However when I change the name of the contract interface, no errors are given (except the same one I'm facing for the last 3 hours)
Does anyone have an idea how to debug this? This kind of blackbox error stuff is very off-putting for me.
full exception:
Service 'WCFtest.TestService' has zero application (non-infrastructure) endpoints. This might be because no configuration file was found for your application, or because no service element matching the service name could be found in the configuration file, or because no endpoints were defined in the service element
(Don't you love these errors which indicate any of 16 possible things which might be wrong)
my program.cs
ServiceHost host;
Type serviceType = typeof(TestService);
host = new ServiceHost(serviceType);
host.Open(); //<---- exception is thrown here
Console.ReadLine();
my test 'wcf service'
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
namespace WCFtest
{
[ServiceContract]
public interface ITest
{
[OperationContract]
double Add(double n1, double n2);
[OperationContract]
double Subtract(double n1, double n2);
[OperationContract]
double Multiply(double n1, double n2);
[OperationContract]
double Divide(double n1, double n2);
}
public class TestService : ITest
{
public double Add(double n1, double n2)
{
double result = n1 + n2;
return result;
}
etc... some methods are removed for brevity
}
}
my app.config
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.serviceModel>
<services>
<service name="WCFtest.testservice"
behaviorConfiguration="testservicebehaviour">
<host>
<baseAddresses>
<add baseAddress="http://localhost:8080/test"/>
</baseAddresses>
</host>
<endpoint address=""
binding="wsHttpBinding"
contract="WCFtest.ITest" />
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="testservicebehaviour">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="False"/>
开发者_如何学Python </behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
It's not a case thing, is it? Your web config says;
WCFtest.testservice
but your code says
WCFtest.TestService
So I suspect the difference in case may do something; usually, C# types are case-sensitive. I'm new to WCF, though...
The "name" attribute of the element in the app.config is case sensitive.
Instead of "WCFTest.testservice", you need to specify "WCFtest.TestService" like so:
<service name="WCFtest.TestService" behaviorConfiguration="testservicebehaviour">
精彩评论