Set between 13th to 15th century C.E. could the people in the medieval age had come up with electric fence to protect the castle? As long as direct unprotected physical contact would produce a fatal shock or burnt to the average healthy adult human and of course the fence must keep the enemy at bay.
Since producing a reliable current is a pain, your fence will consist of wires mounted on wooden poles, placed some distance above ground. The wires will be charged to a high positive potential, using large sheets of silk riveted to plates at the ends of the wires. Stiff brushes mounted on poles connected to a mill (animal, wind or human) will brush against the silk to generate the charge.
The result will be more of a bug-zapper than a traditional fence. People sneaking under the fence will change the capacitance of the gap between the wires and ground and get electrocuted. People trying to climb over will short the wires and get electrocuted. Lots of people trying at the same time will only get a mild shock. People using pikes and hooks to tear down your wires will be unaffected.
The major problem with this system, is once the wires discharge, it'll take a while to build up sufficient charge to be useful again. By that time, you'll be overrun. As a deterrent, it's pointless; as an alarm, it would be priceless: set up a negatively charged needle, attached to a spring and counterweight, near the wires. Once the fence discharges, the spring will pull back the counterweight, and the force can be used to ring a bell or drop a weight onto a sheet of metal, telling your guards that something is wrong.
If your question goes in the direction of how would bringing contemporary knowledge and tech affect the Middle Age you should read the Mark Twain's approach in A Connecticut Yankee in King Arthur's Court
Of course, as noticed in other coments, the only fence would be useless, but if you get to build it, then you can do other cool things as described in the book. If at that time you were able to generate electricity like that, well, then you would be a magician for them.
No. It's not possible. It requires (a) someone with a knowledge of electricity to direct the fabrication and construction of the electrified fence, and the generators to power it, (b) it would be effectively economically and practically to make all the wiring and metal components for conducting and generating electricity.
Yes, Medieval people were good at twisting wire into chain mail. But chain mail was expensive stuff. Its manufacture was labor intensive and the materials were expensive and hard to come by. You would need a bigger army of blacksmiths to make all the wire needed and all the other metal components for the generators, than troops to defend the castle.
All the all, the wise guy from the future who thought of an electrified fence to protect the castle was a dimwit.
Maybe if the castle was by a river so you have hydro power. You need a lot of charge to stop a human, and a lot of metal for that fence. If you have a river and a mine, it might work. Iron sucks as a conductor when fresh out of the ground, but gold fence might be plausible, and much easier to draw into a wire. You'd need one hell of a vein or King Midas to produce enough gold.
Stick with hydro power and some spinning magnets on a water wheel. That'll give you the pulsing as the wheel moves. Maybe someone else can work out the mining/metal problem.
Animals can be fenced in by single-strand electric fences. To stop humans, you need more.
- Producing wire in the required quality and quantity will be difficult.
- The wire has to be mounted on insulators. That used to be porcelain.
- Fences use high voltage pulses. How are you going to generate them?