I was following MSDN tutorial to create the WCF service, when I run it, it is correctly started, but when I follow localhost, the server is blank.
What can I do?
I am using Chrome, I've changed setting as it was suggested in many posts.
ICalculator.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
namespace GettingStartedLib
{
[ServiceContract(Namespace="http://Microsoft.ServiceModel.Samples")]
public interface ICalculator
{
[OperationContract]
double Add(double n1, double n2);
[OperationContract]
double Substract(double n1, double n2);
[OperationContract]
double Multiply(double n1, double n2);
[OperationContract]
double Divide(double n1, double n2);
}
}
CalculatorService
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
namespace GettingStartedLib
{
public class CalculatorService:ICalculator
{
public double Add(double n1, double n2)
{
double result = n1 + n2;
Console.WriteLine("Otrzymano: Add({0},{1})", n1, n2);
Console.WriteLine("Wynik: {0}", result);
return result;
}
public double Substract(double n1, double n2)
{
double result = n1 - n2;
Console.WriteLine("Otrzymano: Substract({0},{1})", n1, n2);
Console.WriteLine("Wynik: {0}", result);
return result;
}
public double Multiply(double n1, double n2)
{
double result = n1 * n2;
Console.WriteLine("Otrzymano: Multiply({0},{1})", n1, n2);
Console.WriteLine("Wynik: {0}", result);
return result;
}
public double Divide(double n1, double n2)
{
double result = n1 / n2;
Console.WriteLine("Otrzymano: Divide({0},{1})", n1, n2);
Console.WriteLine("Wynik: {0}", result);
return result;
}
}
}
Host:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ServiceModel;
using System.ServiceModel.Description;
using GettingStartedLib;
namespace GettingStartedHost
{
class Program
{
static void Main(string[] args)
{
//Krok 1: utworzenie URI, które będzie służyło jako adres bazowy
Uri uri = new Uri("http://localhost:8000/GettingStarted/");
//Krok 2: utworzenie instancji obiektu ServiceHost (hosta serwera)
ServiceHost selfHost = new ServiceHost(typeof(CalculatorService), uri);
try
{
//Krok 3: dodanie punktu końcowego serwera
selfHost.AddServiceEndpoint(typeof(ICalculator), new WSHttpBinding(), "CalculatorService");
//Krok 4: umożliwienie wymiany metadanych
ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
smb.HttpGetEnabled = true;
selfHost.Description.Behaviors.Add(smb);
//Krok 5: uruchomienie hosta serwera
selfHost.Open();
Console.WriteLine("Serwer jest gotowy!");
Console.WriteLine("Naciśnij <ENTER>, by go zamknąć.");
Console.WriteLine();
Console.ReadLine();
//zamknięcie obiektu ServiceHost w celu zamknięcia serwera
selfHost.Close();
}
catch (CommunicationException e)
{
Console.WriteLine("Wystąpił wyjątek: {0}",e.Message);
selfHost.Abort();
}
}
}
}
App.config for library
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
</appSettings>
<system.web>
<compilation debug="true" />
</system.web>
<!-- When deploying the service library project, the content of the config file must be added to the host's
app.config file. System.Configuration does not support config files for libraries. -->
<system.serviceModel>
<services>
<service name="GettingStartedLib.CalculatorService">
<host>
<baseAddresses>
<add baseAddress = "http://localhost:8000/GettingStarted/CalculatorService" />
</baseAddresses>
</host>
<!-- Service Endpoints -->
<!-- Unless fully qualified, address is relative to base address supplied above -->
<endpoint address="" binding="basicHttpBinding" contract="GettingStartedLib.ICalculator">
<!--
Upon deployment, the following identity element should be removed or replaced to reflect the
identity under which the deployed service runs. If removed, WCF will infer an appropriate identity
automatically.
-->
<identity>
<dns value="localhost"/>
</identity>
</endpoint>
<!-- Metadata Endpoints -->
<!-- The Metadata Exchange endpoint is used by the service to describe itself to clients. -->
<!-- This endpoint does not use a secure binding and should be secured or removed before deployment -->
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior>
<!-- To avoid disclosing metadata information,
set the values below to false before deployment -->
<serviceMetadata httpGetEnabled="True" httpsGetEnabled="True"/>
<!-- To receive exception details in faults for debugging purposes,
set the value below to true. Set to false before deployment
to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="False" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
EDIT2:
I followed @OmegaMan suggestion to test it with WCFTestClient. It gave me en error.
But when I tried the address: http://localhost:8000/GettingStarted/ (without CalculatorService even though it is said so in App.config) it works!
Why is that? It's weird.
EDIT3: Ok, I've figured out that I need to add "/" in the *.config file in:
<add baseAddress = "http://localhost:8000/GettingStarted/CalculatorService" />
so it goes like
<add baseAddress = "http://localhost:8000/GettingStarted/CalculatorService/" />
But can anybody tell me why my programming methods don't work? I mean if I change the address in *.config file (or leave it by default) it never works (the mentioned link- altough server starts properly)
Try changing localhost to the actual computer name or IP. I ran into a similar problem with my WCF service
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With