Click here to Skip to main content
15,905,782 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hai ,


I have some doubts in wcf concepts.I have created a service "WCFService" in console application.Then I have created interface"IArea" in a seperate interface file.Then I has implemented the methods into another class names"areaservice".I am trying to run this service but it can't running.I think there is an error in uri base address.I do no lot about base address.How can we put base address and port number.Is there is any default procedure for this.I do no about how we put port number in base address.So plz clear my doubts.Below I have send my code.


Service name->WCFService
Interface->IArea
class->AreaService


AreaService.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WCFService
{
   public class AreaService:IArea 
    {
       public double areaofrectangle(double length, double width)
       {
           double area = length * width;
           Console.WriteLine("length:{0};width:{1}", length, width);
           Console.WriteLine("Area:{0}", area);
           return area;
       }
       public double areaofcircle(double radius)
       {
           double area = Math.PI * radius;
           Console.WriteLine("radius:{0}", radius);
           Console.WriteLine("Area:{0}", area);
           return area;
       }
    }
}


IArea.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;

namespace WCFService
{
    [ServiceContract(Namespace="http://WCFService")]
   public interface IArea
    {
        [OperationContract]
        double areaofrectangle(double length, double width);
        [OperationContract]
        double areaofcircle(double radius);
    }
}


program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Description;

namespace WCFService
{
    class Program
    {
        static void Main(string[] args)
        {
            Uri baseaddress = new Uri("http://localhost:8080/WCFService/Sample");
            ServiceHost selfhost = new ServiceHost(typeof(AreaService), baseaddress);
            try
            {
                selfhost.AddServiceEndpoint(typeof(IArea),new WSDualHttpBinding(),"AreaService");
                ServiceMetadataBehavior smd=new ServiceMetadataBehavior ();
                smd.HttpGetEnabled=true;
                selfhost.Description.Behaviors.Add(smd);
                selfhost.Open();
                Console.WriteLine("service is ready");
                Console.WriteLine("press <enter> key to stop");
                Console.WriteLine();
                Console.ReadLine();
                selfhost.Close();
            }
            catch (CommunicationException ex)
            {
                Console.WriteLine("An Error is occured:{0}", ex.Message);
                selfhost.Abort() ;
            }
        }
    }
}
Posted
Updated 22-Aug-11 15:30pm
v2

1 solution

I would suggest checking your web.config file (or app.config) for more information on the defaults that were set up for you when you created the project.

What errors are you receiving that are leading you to believe it's the base address?

Make sure you are decorating your classes/methods correctly. Make sure your interface exposes the same methods.

Here's an intro with some walkthroughs to get you started.
http://invalidcast.com/2010/04/a-gentle-introduction-to-wcf[^]

Cheers.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900