none
Convert Class Library into WCF Service RRS feed

  • Question

  • Hi,

    I have a class library that uses the Factory Pattern (code below). I want to use this as a WCF service so that it can be called by client applications.

    Is this possible? How do I use the WCF service to call each Build method?

    In the class library solution I have a console project for debugging. I call each report as follows...

    string temporaryFilePath1 = SSRSFactory.GetInstance().BuildTest1Report("1234", "2016/06/30");
    
    string temporaryFilePath2 = SSRSFactory.GetInstance().BuildTest2Report("1029", "2016/06/30");

    Factory Pattern Code:

    	public sealed class SSRSFactory
        {
            public string BuildTest1Report(string id, DateTime period)
            {
                return this.Build(new Test1ReportBuilder(id, period));
            }
            public string BuildTest2Report(DateTime period)
            {
                return this.Build(new Test2ReportBuilder(period));
            }
    
            private string Build(ISSRSBuilder SSRSBuilder)
            {
                string outputFilePath = Path.Combine(@"C:\Temp\", SSRSBuilder.OutputFileName);
                string temporaryFilePath = System.IO.Path.GetTempFileName();
                _logger.DebugFormat("Populating '{0}' with data...", outputFilePath);
    
                try
                {
                    #region ssrs web service
                    ReportService2005.ReportingService2005 rs = new ReportService2005.ReportingService2005();
                    ReportExecution2005.ReportExecutionService rsExec = new ReportExecution2005.ReportExecutionService();
                    #endregion
    
                    // Prepare Render arguments
                    string historyID = null;
                    string deviceInfo = null;
    
                    Byte[] results;
                    string encoding = String.Empty;
                    string mimeType = String.Empty;
                    string extension = String.Empty;
                    ReportExecution2005.Warning[] warnings = null;
                    string[] streamIDs = null;
    
                    // Load the selected report.
                    ReportExecution2005.ExecutionInfo ei = rsExec.LoadReport(SSRSBuilder.ReportName, historyID);
    
                    rsExec.SetExecutionParameters(SSRSBuilder.GetParameterList(), "en-us");
    
                    results = rsExec.Render(SSRSBuilder.OutputType, deviceInfo,
                              out extension, out encoding,
                              out mimeType, out warnings, out streamIDs);
    
                    // set file extension for output
                    string outputFilePathExtension;
                    switch (SSRSBuilder.OutputType)
                    {
                        case "WORD":
                            outputFilePathExtension = ".doc";
                            break;
                        case "PDF":
                            outputFilePathExtension = ".pdf";
                            break;
                        case "EXCEL":
                            outputFilePathExtension = ".xls";
                            break;
                        default:
                            outputFilePathExtension = ".pdf";
                            break;
                    }
    
                    string filename = Path.GetFileNameWithoutExtension(outputFilePath) + outputFilePathExtension;
                    outputFilePath = Path.GetDirectoryName(outputFilePath) + "\\" + filename;
    
                    _logger.DebugFormat("'{0}' output filepath.", outputFilePath);
    
                    // Create a file stream and write the report to it
                    using (FileStream stream = File.OpenWrite(outputFilePath))
                    {
                        stream.Write(results, 0, results.Length);
                    }
                }
                catch (Exception exception)
                {
                    _logger.Error("SSRSFactory Build Error", exception);
                }
                finally
                {
                    try
                    {
                        File.Delete(temporaryFilePath);
                    }
                    catch (Exception exception)
                    {
                        _logger.Error(string.Format("An exception occurred while deleting temporary file '{0}'.", temporaryFilePath), exception);
                    }
                }
    
                return outputFilePath;
            }
        }

    Tuesday, July 19, 2016 3:21 PM

Answers

  • I have a class library that uses the Factory Pattern (code below). I want to use this as a WCF service so that it can be called by client applications.

    Is this possible? How do I use the WCF service to call each Build method?

    The WCF service would have reference to the classlib project. The WCF Operational Contracts would instantiate the classlib project's objects and call methods on the classlib project's objects or call a Factory methods.  WCF's job is about SOA and communications between the WCF client and the service.  It's not the WCF service's job to do the reports. That's the classlib project's job that the WCF service is calling upon.

    The WCF Operational Contracts would be the below that the WCF client would see.

    string temporaryFilePath1 = SSRSFactory.GetInstance().BuildTest1Report("1234", "2016/06/30");

    string temporaryFilePath2 = SSRSFactory.GetInstance().BuildTest2Report("1029", "2016/06/30");

    Find a tutorial using Bing or Google on the WCF service that best fits your needs.

    http://www.codeproject.com/Articles/515253/Service-Oriented-Architecture-and-WCF

    Tuesday, July 19, 2016 4:50 PM