Quantcast
Channel: WSE 3.0 to WCF
Viewing all articles
Browse latest Browse all 4

WSE 3.0 to WCF

$
0
0
Hello, I have Class library created in WSE 3.0 and i need create a class of the same functionality using WCF.

internal class SoapRouter : SoapHttpRouter {

        private static string eventLogSource = "Gateway2";
        private HttpContext context;
        private DateTime requestDateTime;
        private string addressTo;
        private string soapAction;
        private CultureInfo hrCultureInfo;

        public SoapRouter(HttpContext context) {

            if (context == null) {
                throw new ArgumentNullException();
            }
            this.context = context;            
        }//ctor

        protected override Uri ProcessRequestMessage(SoapEnvelope message) {
            Uri uri;
            try {
                //od zahtijeva trebamo requestTime
                this.requestDateTime = DateTime.Now;
                //i adresu servisa            
                this.addressTo = message.Context.Addressing.To.Value.OriginalString;
                //i soap action
                this.soapAction = message.Context.Addressing.Action.Value;
                //i hrCultureInfo
                this.hrCultureInfo = new CultureInfo( "hr-HR" );
                // to je sve sto moramo pamtiti u slucaju da zahtijev ne prodje
                //na request messageu pisemo u event log
                string newLine = Environment.NewLine;
                StringBuilder builder = new StringBuilder();
                builder.Append( "Date: " );
                builder.Append( this.requestDateTime.ToString( "F", this.hrCultureInfo ) );
                builder.Append( newLine );
                builder.Append( "IP: " );
                builder.Append( this.context.Request.UserHostAddress );
                builder.Append( newLine );
                builder.Append( "Addressing.To: " );
                builder.Append( this.addressTo );
                builder.Append( newLine );
                builder.Append( "SoapAction: " );
                builder.Append( this.soapAction );
                builder.Append( newLine );
                builder.Append( "Cert. Serial: " );
                builder.Append( this.context.Request.ClientCertificate.SerialNumber );
                builder.Append( newLine );
                builder.Append( "Cert. Subject: " );
                builder.Append( this.context.Request.ClientCertificate.Subject );
                builder.Append( newLine );
                builder.Append( "UserAgent: " );
                builder.Append( this.context.Request.UserAgent );
                builder.Append( newLine );
                //pokusati upisati novi log u event log            
                EventLog.WriteEntry( SoapRouter.eventLogSource, builder.ToString(), EventLogEntryType.Information );

            }
            catch( Exception ) {
                //Nikakav exception uzrokovan ovom komponentom ne smije omesti rad servisa
            }
            finally {
                //Izvuèemo datum kreiranja Soap poruke
                message.Header.SelectSingleNode("Security/TimeStamp/Created");                                
                DateTime created = DateTime.Parse(message.Header.SelectSingleNode("//*[local-name()='Created']").InnerText);

                DateTime currentDateTime = DateTime.Now;
                //Ako je poruka kreirana van +- sat vremena od vremena na serveru treba je odbaciti
                if (created < currentDateTime.AddHours(-1) || created > currentDateTime.AddHours(1)) {
                    SoapException soapException = null;
                    XmlDocument doc = new XmlDocument();
                    string messageToReturn =  String.Format("Vrijeme, datum ili vremenska zona na Vašem raèunalu je van dopuštenog odstupanja. Molimo da podesite datum i vrijeme na {0} u vremenskoj zoni GMT(+01:00), uz automatsku detekciju promjene ljetnog/zimskog raèunanja vremena.", currentDateTime.ToString());
                    Status status = new Status(1,1000, 1 ,2, 1 ,10, messageToReturn);
                    XmlNode node = doc.CreateNode(XmlNodeType.Element,  SoapException.DetailElementName.Name, SoapException.DetailElementName.Namespace);
                    node.AppendChild(doc.ImportNode(status.GetXml(), true));

                //client
                    soapException = new SoapException(status.Description, SoapException.ClientFaultCode, "http://e-porezna.porezna-uprava.hr/gateway", node);
                throw soapException;

                }

                //i pozovi nadredjeni da odradi svoje
                uri = base.ProcessRequestMessage( message );
            }
            return uri;

        }//ProcessRequestMessage()

        protected override void ProcessResponseMessage(SoapEnvelope message) {
            try {
                //samo ako HTTP kod nije 200, radimo logging!
                if( this.context.Response.StatusCode != 200 ) {

                    DateTime responseDateTime = DateTime.Now;

                    Dictionary<string, string> keys = new Dictionary<string, string>();
                    keys.Add( "%%GUID%%", Guid.NewGuid().ToString() );
                    keys.Add( "%%SERVER%%", Environment.MachineName );
                    keys.Add( "%%REQUESTTIME%%", this.requestDateTime.ToString( "F", this.hrCultureInfo ) );
                    keys.Add( "%%RESPONSETIME%%", responseDateTime.ToString( "F", this.hrCultureInfo ) );
                    keys.Add( "%%IPADDRESS%%", this.context.Request.UserHostAddress );
                    //contentlength he int, a treba nam string
                    string contentLength = Convert.ToString( this.context.Request.ContentLength, this.hrCultureInfo );
                    keys.Add( "%%CONTENTLENGTH%%", contentLength );
                    keys.Add( "%%REQUESTURL%%", this.context.Request.Url.OriginalString );
                    keys.Add( "%%SERVICEADDRESS%%", this.addressTo );
                    keys.Add( "%%USERAGENT%%", this.context.Request.UserAgent );
                    keys.Add( "%%SOAPACTION%%", this.soapAction );

                    //statusCode je takodjer int, a treba nam string
                    string statusCode = Convert.ToString( this.context.Response.StatusCode, this.hrCultureInfo );
                    keys.Add( "%%HTTPSTATUSCODE%%", statusCode );
                    keys.Add( "%%HTTPSTATUSMESSAGE%%", this.context.Response.StatusDescription );
                    //idemo sad vidjeti da li mozemo parsirati SoapMessage
                    SoapException soapException = message.Fault as SoapException;
                    StatusCollection statusList = new StatusCollection();
                    if( soapException != null ) {

                        if( soapException.Message != null ) {
                            
                            string soapExceptionMessage = soapException.Message;
                            bool trimNeeded = 80 < soapExceptionMessage.Length ? true : false;
                            if( trimNeeded == true ) {
                                soapExceptionMessage = soapExceptionMessage.Substring( 0, 80 );
                                soapExceptionMessage += " ... [skraæeno]";
                            }
                            keys.Add( "%%SOAPFAULTMESSAGE%%", soapExceptionMessage );
                        }//if (soapException.Message != null)...

                        //dakle, imamo SoapException, pa idemo vidjeti koji je kod bio
                        string soapFaultCode = "SOAP:VersionMismatch";
                        if( SoapException.IsClientFaultCode( soapException.Code ) == true ) {
                            soapFaultCode = "SOAP:Client";
                        }
                        else if( SoapException.IsServerFaultCode( soapException.Code ) == true ) {
                            soapFaultCode = "SOAP:Server";
                        }
                        else if( SoapException.IsMustUnderstandFaultCode( soapException.Code ) == true ) {
                            soapFaultCode = "SOAP:MustUnderstand";
                        }
                        keys.Add( "%%SOAPFAULTCODE%%", soapFaultCode );

                        //mozda SoapException sadrzi nas ePorezna Status
                        XmlElement detailElement = soapException.Detail as XmlElement;

                        if( detailElement != null ) {....
This is just part of a class.

Please advice.

Viewing all articles
Browse latest Browse all 4

Latest Images

Trending Articles





Latest Images