آشنایی با Message Contract در سرویس WCF

دوشنبه 12 بهمن 1394

در این مقاله درباره Message Contract یا قرارداد پیام در WCF برای کنترل پیام های SOAP صحبت کرده و نحوه استفاده از آن را در سرویسهای WCF بررسی خواهیم کرد.

آشنایی با  Message Contract در سرویس WCF

در این مقاله درباره  Message Contract  یا قرارداد پیام در WCF برای کنترل پیام های  SOAP صحبت کرده و نحوه استفاده از آن را در سرویسهای  WCF بررسی خواهیم کرد.

 در صورت تمایل میتوانید به صورت رایگان آموزش WCF به همراه پروژه عملی را در سایت تاپ لرن مشاهده کنید .

message contract یا قرارداد پیام برای کنترل پیام های SOAP بین یک سرویس WCF و ارتباطات مشتری است که به دو روش زیر استفاده می شود:

1. اضافه کردن داده های سفارشی به soap header . برای مثال اضافه کردن اطلاعات امنیتی کاربر یا اطلاعات مجوزها

2. تغییر نام عنصر Wrapper از پیام Soap و اضافه یا حذف کردن بعضی عناصر آن .

بنابراین قرارداد پیام فقط برای ایجاد تغییرات در پیام SOAP استفاده می شود. به مثال زیر توجه کنید.

برای این سرویس ، پیام  SOAP مشتری به صورت زیر خواهد بود:

Request message

    <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">  
        <s:Header>  
            <ActivityId CorrelationId="6d5cd048-ae4a-4631-b3b8-34ac8bb97400" xmlns="http://schemas.microsoft.com/2004/09/ServiceModel/Diagnostics">4310f382-b1b2-4829-80a7-56ac725ffd80</ActivityId>  
        </s:Header>  
        <s:Body>  
            <GetStudent xmlns="http://tempuri.org/">  
                <name>ahmadi</name>  
            </GetStudent>  
        </s:Body>  
    </s:Envelope>  

Response message

    <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">  
        <s:Header></s:Header>  
        <s:Body>  
            <GetStudentResponse xmlns="http://tempuri.org/">  
                <GetStudentResult xmlns:a="http://schemas.datacontract.org/2004/07/MessageContract" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">  
                    <a:Department>MCA</a:Department>  
                    <a:Name>ahmadi</a:Name>  
                    <a:Year>3</a:Year>  
                </GetStudentResult>  
            </GetStudentResponse>  
        </s:Body>  
    </s:Envelope>  

پیام را به صورت زیر تغییر می دهیم.

Request message

    <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">  
        <s:Header>  
            <h:SecurityKey xmlns:h="http://localhost/schema">1234</h:SecurityKey>  
            <ActivityId CorrelationId="ac7ec340-b578-4bc4-bb30-43a112d26e54" xmlns="http://schemas.microsoft.com/2004/09/ServiceModel/Diagnostics">3d00e695-78bf-4bcb-9a41-05a2e45ef9fa</ActivityId>  
        </s:Header>  
        <s:Body>  
            <StudentRequest xmlns="http://localhost/schema">  
                <StudentName>ahmadi</StudentName>  
            </StudentRequest>  
        </s:Body>  
    </s:Envelope>  

Response message


    <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">  
        <s:Header></s:Header>  
        <s:Body>  
            <StudentResponse xmlns="http://localhost/schema">  
                <Department>MCA</Department>  
                <StudentName>ahmadi</StudentName>  
                <Year>3</Year>  
            </StudentResponse>  
        </s:Body>  
    </s:Envelope>  

برای این کار لازم است تا قرارداد پیام را پیاده سازی کنیم . برای پیاده سازی آن طبق مراحل زیر پیش می رویم.

دو کلاس با نام های StudentRequest  و StudentResponse  ایجاد میکنیم. کلاس StudentRequest برای ارسال درخواست به سرویس WCF و کلاس  StudentResponse برای دریافت پاسخ از  WCF استفاده می شود.

کدهای زیر را در کلاس ها اضافه میکنیم.

StudentRequest.cs

 [MessageContract(IsWrapped = true, WrapperName = "StudentRequest", WrapperNamespace = "http://localhost/schema")]
   public class StudentRequest
    {
        [MessageHeader( Namespace = "http://localhost/schema")]
        public string SecurityKey { get; set; }

        [MessageBodyMember(Namespace = "http://localhost/schema")]
        public string StudentName { get; set; }
    }

StudentResponse.cs

[MessageContract(IsWrapped = true, WrapperName = "StudentResponse", WrapperNamespace = "http://localhost/schema")]
    public class StudentResponse
    {
        [MessageBodyMember(Namespace = "http://localhost/schema")]
        public string StudentName { get; set; }

        [MessageBodyMember(Namespace = "http://localhost/schema")]
        public string Department { get; set; }

        [MessageBodyMember(Namespace = "http://localhost/schema")]
        public int Year { get; set; }


    }

کدهای سرویس را نیز به صورت زیر تغییر می دهیم.

StudentService.cs

 public class StudentService : IStudentService
    {
        public StudentResponse GetStudent(StudentRequest request)
        {
            switch(request.StudentName)
            {
                case "ahmadi": return new StudentResponse { StudentName = "ahmadi", Department = "MCA", Year = 3 };
                case "rezaei": return new StudentResponse { StudentName = "rezaei", Department = "BCA", Year = 3 };
                default: return new StudentResponse { StudentName = request.StudentName, Department = "BE", Year = 3 };
            }
        }
    }

و IStudentService.cs

 [ServiceContract]
    public interface IStudentService
    {
        [OperationContract]
        StudentResponse GetStudent(StudentRequest request);
    }

در کلاس مشتری نیز کدها به صورت زیر می باشد :

static void Main(string[] args)
        {
            ServiceRef.IStudentService client = new ServiceRef.StudentServiceClient();
            ServiceRef.StudentRequest request = new ServiceRef.StudentRequest();
            request.StudentName = "ahmadi";
            request.SecurityKey = "1234";
            ServiceRef.StudentResponse studentResponse = client.GetStudent(request);
            Console.WriteLine("Name : " + studentResponse.StudentName);
            Console.WriteLine("Department : " + studentResponse.Department);
            Console.WriteLine("Year : " + studentResponse.Year);
            Console.ReadKey();
        }

در اینجا از اینترفیس بجای کلاس مشتری استفاده شده است . اگر میخواهید از Message Contract استفاده کنید ، ارتباطات باید از طریق اینترفیس اتفاق بیفتند.

تفاوت مهم بین  Data contract  و  Message Contract این است که data contract تا اندازه ای پیام  SOAP را کنترل میکند. اما Message Contract  کنترل کاملی بر روی پیام های SOAP دارد.

فایل ضمیمه را در ویژوال استودیو از طریق  Administrator اجرا کنید.

فایل های ضمیمه

برنامه نویسان

نویسنده 3355 مقاله در برنامه نویسان
  • WCF
  • 2k بازدید
  • 3 تشکر

کاربرانی که از نویسنده این مقاله تشکر کرده اند

در صورتی که در رابطه با این مقاله سوالی دارید، در تاپیک های انجمن مطرح کنید