本文介绍了WCF中的基址问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

海,


我对wcf概念有些疑问.我在控制台应用程序中创建了服务"WCFService".然后在单独的接口文件中创建了接口"IArea".然后将方法实现为另一个类名"areaservice".尝试运行此服务,但无法运行.我认为uri基址有错误.我对基址没有太多要求,如何放置基址和端口号,是否有任何默认程序?我不知道如何将端口号放入基址中,所以请清除我的疑问.下面我发送了我的代码.


服务名称-> WCFService
界面-> IArea
class-> AreaService


AreaService.cs

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



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



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() ;
            }
        }
    }
}

推荐答案


这篇关于WCF中的基址问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-14 21:03