Showing posts with label Programming. Show all posts
Showing posts with label Programming. Show all posts

Tuesday, September 06, 2011

"The specified metadata path is not valid." error in using Windows Service and Entity Framework

I have project (Windows Service) that use Entity Framework model as external DLL.
I cannot use a metadada as embedded resource and must define connection string in config file of Windows Service. In the Debug mode I got right result, I created setup project and installed service in directory but a immediately I saw the next error:

 The specified metadata path is not valid. A valid path must be either an existing directory, an existing file with extension '.csdl', '.ssdl', or '.msl', or a URI that identifies an embedded resource.
I'll not tell you what I not tried  and a solution was found after deep research in order to understand where is run Windows Service. Bad news! Windows Service not run in installed directory therefore all path's to files in config file must be define as physical path otherwise application cannot find it. Windows service use relative path different to installation directory.

Where you use Entity Framework you must to define row as this one:
...metadata=.\ServiceModel.csdl|.\ServiceModel.ssdl|.\ServiceModel.msl...
In order to not change path to physical path just add next row in event OnStart in Windows Service:
System.IO.Directory.SetCurrentDirectory(System.AppDomain.CurrentDomain.BaseDirectory);

This is a way to define relative path of Service as installed directory.

Enjoy!

kick it on DotNetKicks.com

Tuesday, June 29, 2010

Generate random string

Current code is generate string in defined length and include chars, numbers and symbols.

public static string GetRandomString(int size)
{
StringBuilder builder = new StringBuilder();
Random random = new Random();
int[] symbol = new[] { 33, 36, 38, 64, 94 };
int[] digits = new[] { 48, 49, 50, 51, 52, 53, 54, 55, 56, 57 };
int symbolsAmount = size / 3;
int digitAmount = size / 3;
for(int i = 0; i < size; i++)
{

int switcher = random.Next(3);
char ch;
if(switcher == 0 && symbolsAmount > 0)
{
int id = random.Next(symbol.Length);
ch = Convert.ToChar(symbol[id]);
symbolsAmount--;
}
else if(switcher == 1 && digitAmount > 0)
{
int id = random.Next(digits.Length);
ch = Convert.ToChar(digits[id]);
digitAmount--;
}
else
{
bool isLower = random.Next(2) == 0 ? false : true;
ch = Convert.ToChar(
Convert.ToInt32(
Math.Floor(26 * random.NextDouble() + ((isLower) ? 65 : 97))
)
);

}
builder.Append(ch);
}

return builder.ToString();
}

Monday, June 21, 2010

Thursday, February 11, 2010

Twice calling Render in ASP.NET page

I have web page, which use HtmlTextWriter for creating HTML in client. List<T> get data from DB and start in loop to add HTML tags, attributes and more.  I hoped to create page with div’s, images and text, but in the debug phase i saw the Render() method called twice. After long tests and researches was found out what is a problem. In creating image <img>, a attribute “src” get value from DB and in one images “accidently” a value was …null. You can say “so what?” A problem exist not in HTML page, a problem concealing in IIS. If page has attribute <…src=”” …/> or<… src=”#” …> that makes IIS load the page twice. Good luck!


kick it on DotNetKicks.com

Sunday, July 12, 2009

C# And Accepting Parameters

Have you ever written a function that looked similar to the following – Passing in an array of a value? To interesting solution click here.

 

via DotNetKicks.com

Sunday, March 09, 2008

Singleton Factory in C#

    // this is the class for which
// I want to maintain a single instance
public class MyClass
{
private MyClass()
{
/* private constructor ensures that
callers cannot instantiate an
object using new() */
}
}

// Singleton factory implementation
public static class Singleton<T> where T : class
{
// static constructor,
//runtime ensures thread safety
static Singleton()
{
// create the single instance
// of the type T using reflection
Instance = (T)Activator.CreateInstance(
typeof(T),true);
}

// serve the single instance to callers
public static T Instance { private set; get; }
}

class Program
{
public static void Main()
{
// test
Console.WriteLine(
Object.ReferenceEquals(
Singleton<MyClass>.Instance,
Singleton<MyClass>.Instance));
}
}




via Cognitive Coding

Sunday, February 10, 2008

Static Extension Methods

DateTime d = DateTime.Yesterday();
//...
public static DateTime Yesterday<this DateTime>()
{
return DateTime.Today.AddDays(-1);
}
 


Via Mabstrerama

Tuesday, February 05, 2008

101 Design Patterns & Tips for Developers

Full guide for developer how to create your application in the using   a "design patterns", that include next parts:

  • Creational Patterns
  • Structural Patterns
  • Behavioral Patterns
  • Composing Methods of Refactoring
  • Moving Features Between Objects
  • Organizing Data
  • Simplifying Conditional Expressions
  • Making Method Calls Simpler
  • Dealing with Generalization
  • Big Refactorings

Click here to read.

Sunday, December 23, 2007

What is the difference between URL and URI?

A URL is the address of some resource on the web, which means that normally you type the address into a browser and you get something back. There are other type of resources than web pages, but that's the easiest conceptually. The browser goes out somewhere on the internet and accesses something.

A URI is just a unique string that uniquely identifies something, commonly a namespace. Sometimes they look like a URL that you could type into the address bar of your web browser, but it doesn't have to point to any physical resource on the web.

URI is the more generic term, and a URL is a particular type of URI in that a URL has to uniquely identify some resource on the web.

Via .Net Tip of The Day

Wednesday, November 21, 2007

Visual Studio 2008 and .NET Framework 3.5 Training Kit

The Visual Studio 2008 and .NET Framework 3.5 Training Kit includes presentations, hands-on labs, and demos. This content is designed to help you learn how to utilize the Visual Studio 2008 features and a variety of framework technologies including: LINQ, C# 3.0, Visual Basic 9, WCF, WF, WPF, ASP.NET AJAX, VSTO, CardSpace, SilverLight, Mobile and Application Lifecycle Management.

Download Page

kick it on DotNetKicks.com

Thursday, November 15, 2007

Add automatic updates to your application

This article explain  step-by-step how to add automatic update capabilities to application quickly and easily.

Click here

kick it on DotNetKicks.com

Sunday, November 11, 2007

Template Method Design Pattern vs. Functional Programming

Simple and obvious example how to realize Design Patterns. A example based on Hot Drinks and presented design of the application with explanations.

Click here

kick it on DotNetKicks.com

Tuesday, October 23, 2007

Strategy Pattern in C# 2.0

Strategy pattern can very handy when a system designer or architect wants to separate algorithms from the system implementation. Also with strategy pattern approach it is very easy to select between different algorithms on the fly. With the introduction of generics in .NET 2.0 implementing strategy pattern is even more easy.

Click Here

 

 

via DotNetKicks.com

Wednesday, October 10, 2007