如何用Unicode代替“??”";WCF web服务中

本文关键字:WCF web 服务 quot Unicode 何用 代替 | 更新日期: 2023-09-27 18:21:40

服务

ISayHello.cs(接口)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
namespace MyWCFService
{
    [ServiceContract]
    public interface IHelloService
    {
        [OperationContract]
        string SayHello(String name, String language);
    }
}

SayHello.cs(类)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
namespace MyWCFService
{   
    public class HelloService : IHelloService
    {
        public string SayHello(String name, String language)
        {
            switch (language)
            {
                case "en":
                    return "Hello " + name;
                case "ja":
                    return "こんにちは " + name;
                case "ch":
                    return "您好 " + name;
                case "es":
                    return "Hola " + name;
                default:
                    return "Unsupported Language";
            }
        }
    }
}

访问类程序.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
namespace WCFServiceTester
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Enter your name: ");
            string name = Console.ReadLine();
            Console.WriteLine("Enter your language: ");
            string lang = Console.ReadLine();
            HelloServiceClient hsc = new HelloServiceClient();
            Console.WriteLine(hsc.SayHello(name, lang));
            Console.ReadKey();
        }
    }
}

我使用的是Visual Studio 2008,我是C#的新手。当我运行程序.cs时,如果我选择ch(中文)或ja(日语)语言,输出如下:

输入您的姓名:Bruce输入您的语言:ja

Bruce(而不是こんにちはBruce)

您的代码应该支持UNICODE。打印它的控制台字体不是。至少默认情况下不会。将控制台切换为UNICODE字体。

如何用Unicode代替“??”";WCF web服务中

Console.OutputEncoding = System.Text.Encoding.Unicode