System.ServiceModel.Channels.MessageHeader Error

System.ServiceModel.Channels.MessageHeader Error



I'm trying to get the following to work on my machine but I get an error (Cannot create an instance of the abstract class or interface 'System.ServiceModel.Channels.MessageHeader')


using System;
using System.IO;
using System.Reflection;

namespace com.companyname.business

/// <summary>
/// Summary description for SessionCreateRQClient.
/// </summary>
class SessionCreateRQClient

/// <summary>
/// The main entry point.
/// </summary>
[STAThread]
static void Main(string args)

try

// Set user information, including security credentials and the IPCC.
string username = "user";
string password = "password";
string ipcc = "IPCC";
string domain = "DEFAULT";

string temp = Environment.GetEnvironmentVariable("tmp"); // Get temp directory
string PropsFileName = temp + "/session.properties"; // Define dir and file name
DateTime dt = DateTime.UtcNow;
string tstamp = dt.ToString("s") + "Z";

//Create the message header and provide the conversation ID.
MessageHeader msgHeader = new MessageHeader();
msgHeader.ConversationId = "TestSession"; // Set the ConversationId

From from = new From();
PartyId fromPartyId = new PartyId();
PartyId fromPartyIdArr = new PartyId[1];
fromPartyId.Value = "WebServiceClient";
fromPartyIdArr[0] = fromPartyId;
from.PartyId = fromPartyIdArr;
msgHeader.From = from;

To to = new To();
PartyId toPartyId = new PartyId();
PartyId toPartyIdArr = new PartyId[1];
toPartyId.Value = "WebServiceSupplier";
toPartyIdArr[0] = toPartyId;
to.PartyId = toPartyIdArr;
msgHeader.To = to;

//Add the value for eb:CPAId, which is the IPCC.
//Add the value for the action code of this Web service, SessionCreateRQ.

msgHeader.CPAId = ipcc;
msgHeader.Action = "SessionCreateRQ";
Service service = new Service();
service.Value = "SessionCreate";
msgHeader.Service = service;

MessageData msgData = new MessageData();
msgData.MessageId = "mid:20001209-133003-2333@clientofsabre.com1";
msgData.Timestamp = tstamp;
msgHeader.MessageData = msgData;

Security security = new Security();
SecurityUsernameToken securityUserToken = new SecurityUsernameToken();
securityUserToken.Username = username;
securityUserToken.Password = password;
securityUserToken.Organization = ipcc;
securityUserToken.Domain = domain;
security.UsernameToken = securityUserToken;


SessionCreateRQ req = new SessionCreateRQ();
SessionCreateRQPOS pos = new SessionCreateRQPOS();
SessionCreateRQPOSSource source = new SessionCreateRQPOSSource();
source.PseudoCityCode = ipcc;
pos.Source = source;
req.POS = pos;

SessionCreateRQService serviceObj = new SessionCreateRQService();
serviceObj.MessageHeaderValue = msgHeader;
serviceObj.SecurityValue = security;


SessionCreateRS resp = serviceObj.SessionCreateRQ(req); // Send the request

if (resp.Errors != null && resp.Errors.Error != null)

Console.WriteLine("Error : " + resp.Errors.Error.ErrorInfo.Message);


else

msgHeader = serviceObj.MessageHeaderValue;
security = serviceObj.SecurityValue;

Console.WriteLine("**********************************************");
Console.WriteLine("Response of SessionCreateRQ service");
Console.WriteLine("BinarySecurityToken returned : " + security.BinarySecurityToken);
Console.WriteLine("**********************************************");
string ConvIdLine = "convid="+msgHeader.ConversationId; // ConversationId to a string
string TokenLine = "securitytoken="+security.BinarySecurityToken; // BinarySecurityToken to a string
string ipccLine = "ipcc="+ipcc; // IPCC to a string

File.Delete(PropsFileName); // Clean up
TextWriter tw = new StreamWriter(PropsFileName); // Create & open the file
tw.WriteLine(DateTime.Now); // Write the date for reference
tw.WriteLine(TokenLine); // Write the BinarySecurityToken
tw.WriteLine(ConvIdLine); // Write the ConversationId
tw.WriteLine(ipccLine); // Write the IPCC
tw.Close();

//Console.Read();



catch(Exception e)

Console.WriteLine("Exception Message : " + e.Message );
Console.WriteLine("Exception Stack Trace : " + e.StackTrace);
Console.Read();








I have added the reference System.ServiceModel and the lines:


System.ServiceModel


using System.ServiceModel;
using System.ServiceModel.Channels;



but I continue to get that error when trying to compile --



Cannot create an instance of the abstract class or interface 'System.ServiceModel.Channels.MessageHeader'


Cannot create an instance of the abstract class or interface 'System.ServiceModel.Channels.MessageHeader'



I am using Microsoft Visual Studio 2008
Version 9.0.21022.8 RTM
Microsoft .NET Framework
Version 3.5 SP1
Professional Edition



Is there another reference I have to add? Or a dll to move over?



I wonder was the code above written for Framework 2.0 only?



Thanks for your help.





What line is the error being thrown on? Please fix the formatting as well. Edit - I see it now I think.
– asawyer
Dec 30 '10 at 16:44






MessageHeader msgHeader = new MessageHeader();
– user220511
Dec 30 '10 at 17:53




1 Answer
1



The error message is correct, you cannot create an instance of that class, it is declared abstract.



http://msdn.microsoft.com/en-us/library/system.servicemodel.channels.messageheader.aspx



See if the static method MessageHeader.CreateHeader will work for you. Note there is several overloads, so pick the best one for you.





Thanks for your answer. I'm stuck using CreateHeader though because don't I still have to make a new MessageHeader() like this (where the first error occurs)?: MessageHeader msgHeader = new MessageHeader();
– user220511
Dec 30 '10 at 18:11






MessageHeader is an abstract class. The framework is telling you that you are never allowed to directly create this an instance of this class. Instead the CreateHeader call will create the instance for you. This was probably done to ensure that the MessageHeader object is initilized properly, and/or CreateHeader will return a polymorphic version of MessageHeader depending on the supplied arguments to CreateHeader. Use a reflector to take a peek into CreateHeader if your really curious. In reality this is an implementation detail that your code should never have to deal with or even be aware of
– asawyer
Dec 30 '10 at 18:20






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)